• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Rotate sprite when move direction is changed

M

Muvvin

Guest
Hey guys,

I'm pretty new to programming and recently got a problem which I can't solve on my own:
if (horizontalgeschw != 0 || vertikalgeschw != 0) {
while (angle_difference(direction,image_angle) < 0) {
image_angle += 2;
}
while (angle_difference(direction,image_angle) < 0) {
image_angle -= 2;
}
}
else {
image_angle = image_angle;
}

It doesn't give me any errors, but the sprite just doesn't rotate when I change the direction of movement to up etc.
I just want to get a smooth rotation, not an instant flip to the new direction.
Any ideas? Thanks for your help, show mercy, I'm new here :D

Cheers
 
D

Dracodino300

Guest
Two things you want to change there:
1. You have "angle_difference < 0" for both statements. You want one to be > 0 (though I can't remember which).
2. If you want smooth rotation. Change the "while" statements to "if". Having while there is just a slow way of having instant rotation.

Also, you don't need that else statement there. It won't change anything by removing it.
 
Z

zendraw

Guest
just do
var adf=angle_difference(direction, image_angle);
if (abs(adf)<=rotation_speed) {image_angle=direction}
else
{
image_angle+=sign(adf)*rotation_speed;
}

not sure what the if check is for but perhaps you can remove it. if you dont know what sign() or abs() is then check the manual. rotation_speed in your case is 2.
 
M

Muvvin

Guest
Thanks for the replies guys, my mistakes are very embarrassing, didn't look through my code carefully enough. Fixed the mistakes, but it still doesn't work ...
The suggested code fom Vile Rack introduced 2 new tools to me (thanks for that), and after understanding your code it does make sense and just has to work, ... but it didn't? I'm so confused, is there some bigger problem with my whole step event code?
 
M

Muvvin

Guest
I tested "direction = 90;" and the sprite started as expected facing up, but when I moved around it kept this state, but I didn't implement "image_angle = direction;"? So maybe the problem has something to do with the direction value?
 
M

Muvvin

Guest
Okay, finally found my mistake ... the direction value isn't set automatically, it has to be set according to the keys being pressed (W,A,S,D). Thanks for your help, today I learned some important things ...
 
Top