GameMaker: Studio

GameMaker: Studio

Find, play, and make games easily
Discover, rate, and download the best player-created games made in GameMaker: Studio for free. Or try making your own and share with the community. Click here to learn more.
TashaRules93 30 Mar, 2018 @ 6:42pm
Basic Coding Question
Hello All,

Thanks for the replies on my previous question for Imaging Programs to use, it was a huge help and turned out to be a success.

I figured I would roll the dice once more and ask a fairly basic coding question that I need help on. I'm fairly new at coding and I'm expanding my horizons by watching tons of tutorials, however one question I could not find the answer for and here it is.

I'm trying to make my main player shoot in a specific direction while faced in that direction using key bindings (standard WASD) I am able to make it shoot in one direction but when I flip my character to the left (A Key) the projectile sprite still goes to the other direction (D Key Right)

below I have the basic coding that I'm using for the action when I click the spacebar;

weapon = instance_create(x,y,obj_weapon);

weapon.direction = image_angle;

weapon.speed = 5;

the character was made to flip directions using the drag and drop mechanic from the program having the A Key flipped horizontally when pressed making the image flip in that direction. I've watched all tutorials on origins and having your projectile point in the direction that your character is pointing, but maybe my origins could be wrong or it could be written in that line of code?

If anyone is able to point me in the right direction it would be greatly appreciated!

Cheers!
< >
Showing 1-6 of 6 comments
melon king 4 31 Mar, 2018 @ 6:15am 
Flipped horizontally? From what you've wrote I assume you're using the drag'n'drop 'Transform a sprite' action to flip the sprite, which doesn't actually change the object's image_angle. Because of this, image_angle is still 0 no matter what you press or do (0 degrees corresponds to the 'east', which is where your projectile seems to be shooting toward).

The solution might be to replace the 'Transform a sprite' action with the 'Execute a piece of code' action with the following code inside:
image_angle = 180;
This example is for your "press A key" event, so for the D key, use 0 degrees instead of 180.

Hopefully that works.
Last edited by melon king; 31 Mar, 2018 @ 6:16am
TashaRules93 31 Mar, 2018 @ 9:49am 
Originally posted by MiNDbreak:
Flipped horizontally? From what you've wrote I assume you're using the drag'n'drop 'Transform a sprite' action to flip the sprite, which doesn't actually change the object's image_angle. Because of this, image_angle is still 0 no matter what you press or do (0 degrees corresponds to the 'east', which is where your projectile seems to be shooting toward).

The solution might be to replace the 'Transform a sprite' action with the 'Execute a piece of code' action with the following code inside:
image_angle = 180;
This example is for your "press A key" event, so for the D key, use 0 degrees instead of 180.

Hopefully that works.

OMG! thank you! I was debating writing a completely different code to fix the problem but this may work and it makes more sense then what I was going to do!
TashaRules93 31 Mar, 2018 @ 10:49am 
Originally posted by MiNDbreak:
Flipped horizontally? From what you've wrote I assume you're using the drag'n'drop 'Transform a sprite' action to flip the sprite, which doesn't actually change the object's image_angle. Because of this, image_angle is still 0 no matter what you press or do (0 degrees corresponds to the 'east', which is where your projectile seems to be shooting toward).

The solution might be to replace the 'Transform a sprite' action with the 'Execute a piece of code' action with the following code inside:
image_angle = 180;
This example is for your "press A key" event, so for the D key, use 0 degrees instead of 180.

Hopefully that works.

When I used the angle_image = 180 it just turned my guy upside down but I came up with something a little better, it took me a little while to understand if statements but this is the code I used to make it go in the opposite direction.

weapon = instance_create(x,y,obj_weapon);
weapon.speed = 5
if (image_xscale = -1)weapon.speed = -5;
melon king 4 1 Apr, 2018 @ 1:43am 
Great to hear, your improved solution is probably much better than using image_angle because it looks like you would prefer the sprite to be flipped and not rotated (I had assumed you might want the character to rotate towards other directions, like up and down). Just note that if you want the character to face more directions than just left and right it would probably be better to use image_angle.

Also, good job on finding out image_xscale and using that to flip a sprite.

Good luck on your future coding endeavours :)

The Winter Bud 4 28 Aug, 2018 @ 7:32am 
Originally posted by tasha_smad:
Originally posted by MiNDbreak:
Flipped horizontally? From what you've wrote I assume you're using the drag'n'drop 'Transform a sprite' action to flip the sprite, which doesn't actually change the object's image_angle. Because of this, image_angle is still 0 no matter what you press or do (0 degrees corresponds to the 'east', which is where your projectile seems to be shooting toward).

The solution might be to replace the 'Transform a sprite' action with the 'Execute a piece of code' action with the following code inside:
image_angle = 180;
This example is for your "press A key" event, so for the D key, use 0 degrees instead of 180.

Hopefully that works.

When I used the angle_image = 180 it just turned my guy upside down but I came up with something a little better, it took me a little while to understand if statements but this is the code I used to make it go in the opposite direction.

weapon = instance_create(x,y,obj_weapon);
weapon.speed = 5
if (image_xscale = -1)weapon.speed = -5;
weapon.speed=image_xscale*5;
Last edited by The Winter Bud; 28 Aug, 2018 @ 7:33am
The Winter Bud 4 11 May, 2024 @ 10:44am 
//left key pressed event image_xscale=-1; weapon.direction=180; weapon.speed=5;
//right key pressed event image_xscale=1; weapon.direction=0; weapon.speed=5; // this is the proper way to move an object

remember that direction is a variable that moves an object in a certain direction on teh screen while image_angle is the rotation of a sprite. You use speed along with direction to make an object move in a certain direction. You would use a negative speed if you want the object to move opposite of direction
example
direction=0; speed=5; // this will go to the right
direction=0; speed=-5; // this will go to the left
direction=180; speed=5; // this will go to the left
Last edited by The Winter Bud; 11 May, 2024 @ 10:51am
< >
Showing 1-6 of 6 comments
Per page: 1530 50