Question: How do you create and manage animations in GameMaker?


GameMaker Studio offers several ways to create and manage animations for your game characters or objects. Here's a comprehensive guide on how to do it:

Sprite Animation

First, animations in GameMaker are typically handled through sprites. A sprite is composed of one or more sub-images. These sub-images can represent different frames of an animation.

Creating an Animated Sprite

  1. Open the Sprite Editor by creating a new sprite resource.
  2. Import or create each frame of your animation as a sub-image.
// To create a new sprite programmatically: var spr_player = sprite_create_from_surface(surf_id, 0, 0, width, height, false, false, width/2, height/2);

Animating the Sprite

Assign the animated sprite to an instance (an object in your game) that should be animated.

object.sprite_index = spr_player; // Assign the sprite to the object object.image_speed = 0.2; // Set the speed of the animation

The image_speed property defines how quickly the animation plays. Setting it to 1 will change the sub-image each step at the room's frame rate. Lower values slow down the animation, while values higher than 1 can speed it up.

Controlling Animation Programmatically

You can control the animation using GML (GameMaker Language), by manipulating properties like image_index, image_speed, and image_xscale.

// Example: Flipping the sprite horizontally when moving left if (keyboard_check(vk_left)) { instance.image_xscale = -1; // Flip horizontally } else if (keyboard_check(vk_right)) { instance.image_xscale = 1; // Normal orientation } // Example: Change to a different animation when jumping if (player_is_jumping) { instance.sprite_index = spr_player_jump; }

Animation End Event

To detect when an animation has finished, you can use the Animation End event of an object. Within this event, you might want to change to another animation or perform some action.

Using Image Index

Each sub-image in a sprite is numbered starting from 0. You can directly set the image_index of an object to display a specific frame of its sprite.

instance.image_index = 3; // Show the fourth frame of the sprite

By manipulating the image index, you can also create more complex animations that don't just play from start to end.

Tips for Smooth Animations

  • Ensure consistent timing between frames for smooth motion.
  • Use easing functions to handle acceleration and deceleration in animations.
  • Consider using skeletal animation tools like Spine or DragonBones, which can be integrated with GameMaker for more complex animations.

In conclusion, GameMaker provides the functionality necessary to import, create, and control sprite-based animations easily. By managing image indexes and animation speeds, you can create dynamic and responsive animations suited to your game's needs.

Was this content helpful?

White Paper

Free System Design on AWS E-Book

Download this early release of O'Reilly's latest cloud infrastructure e-book: System Design on AWS.

Free System Design on AWS E-Book

Start building today

Dragonfly is fully compatible with the Redis ecosystem and requires no code changes to implement.