How to change the animation speed of a sprite in Game Maker?

Hi and welcome to this tutorial in French on Game Maker Studio. Today, we’ll see how to stop, slow down, pause, or even speed up the animation of a sprite (you know, those little images that represent the characters, objects, and backgrounds of your game).

When we use assets in our game, it’s possible to animate them to give them life. However, there may be times when we want to stop this animation at a specific moment. For that, we can use the variable image_speed.

What is image_speed?

image_speed, as the name suggests, controls the animation speed of the images of a sprite. This variable is what GMS calls a “built-in variable”, meaning it’s automatically defined by GameMaker Studio in every sprite we create. In fact, it’s possible to identify this type of variable because their names are written in green by default in our IDE.

This variable acts as a multiplier of the number of images per second (FPS) of our sprite, and its default value is 1. If the speed of your sprite is 10FPS and image_speed is 2, then the animation plays at the following speed: 10FPS * 2 = 20FPS. This way, your asset will be animated twice as fast.

// To change the speed of an animation
image_speed = 1; // default (100%)
image_speed = 2; // 2x faster (200%)
image_speed = .5; // 2x slower (50%)
image_speed = -3; // reverse direction and 3x faster (-300%)

If you want to see the entire process, here’s the tutorial video in French that I created for the occasion.

Going Further with Animations

If we want to stop the animation more gradually, we can use this line in your Step event: image_speed -= delta. This reduces the animation speed by delta each time we go through the step event, allowing for a smoother transition to complete stop when image_speed equals 0.

Similarly, if we want to start the animation only when our character moves, we should write:

/ Step event
// the variable state contains the state of our character

if(state == "run") {
    image_speed = 1;
} else {
    image_speed = 0,
}

Conclusion

And there you have it! Now, you know how to stop the animation of a sprite in Game Maker Studio. This trick can be useful in many situations, so feel free to use it in your next project. Thanks for reading this tutorial, and see you soon for more Game Maker tips!

Subscribe to my newsletter - A Good Interface


Leave a Reply

Your email address will not be published. Required fields are marked *