• 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!

Toggle on/off sprite animation

KKAddi

Member
What I planned to create:
When I press the key "E", the sprite will play from the first frame, and when it reaches the last frame, it will stop.
When I press "E" again, the sprite will play reversely from the last frame till the first frame and stop.

Create:
GML:
image_speed = 0;
on_off = 0;
Key Press - E
GML:
if distance_to_object(obj_player1) <= 50 {
if on_off = 0 {
    on_off = 1;
}
if on_off = 1 {
    on_off = 2;
}
}
Step
GML:
if on_off = 1 {
    image_speed = 1;
    if image_index = 2 {
        image_speed = 0;
    }
    on_off = 0
  
}
if on_off = 2 {
    image_speed = -1
    if image_index = 0 {
        image_speed = 0;
    }
    on_off = 0
}
But when I walk towards the lamp(within the 50 pixel radius), and pressed "E", nothing happens.
Anyone can help?
 

Attachments

Nidoking

Member
You'll probably get a lot closer to what you want by eliminating the on_off variable. It only ever holds a value for one step, because you're setting it back to 0 immediately. So it will never be set to 2. I would just set the animation speed directly and consider using the Animation End event to stop it at the last frame. You'll also probably want to use a debug statement or breakpoint to make sure the keypress event is actually getting into the distance_to_object check.
 

Ommn

Member
try this:
GML:
if distance_to_object(obj_player1) <= 50 {
if on_off = 0 {
    on_off = 1;
}
else if on_off = 1 {
    on_off = 2;
}
}
 
Top