Sprites
This page is about how Game Maker uses sprites. If you would like to find free sprites or learn to make sprites you can use in your game, see our Game Sprites Wiki

A sprite is an image, or a number of images, that represents something in the game. The sprite can represent anything from a game character to castle wall — whatever the sprite itself looks like. If a sprite is not animated (a tree in an RPG for example), it only has one image. If it is animated, the sprite has a number of subimages.

Actions

Events

Many games use multiple sprites with different animations each to represent one character performing different actions such as walking, jumping and falling. The sprite displayed by the object can be changed by changing the image_index of the object.

Drawing Sprites

When a sprite is set using sprite_index or using the Object Properties Window, an instance will draw it's own sprite at it's own position.

Sprite Origin

The sprite origin is the handle by which determines the sprite's location. It is a single point on the sprite. When you move the sprite, you are really moving the sprite origin, and the sprite "follows". You can see where the sprite origin is on the sprite itself by checking the read only variables, sprite_xoffset and sprite_yoffset.

There is no option to change the sprite origin in the Sprite Properties window in Simple Mode. You must be in advanced mode to see the options.

Changing the Sprite of an Instance

To change the sprite of an instance, use sprite_index.

sprite_index = spr_another_sprite;

Depth and Drawing Order

Depending on the depth of the instance, it will be drawn in front of or behind other instances. Instances with higher depth will be drawn first, instances with lower depths are drawn last, thus, those with higher depths appear behind instances with lower depths.

Random Sprites

You can choose between sprites randomly using several different methods. If you have only a few sprites to choose from the best way is to use choose().

random_sprite = choose(sprite0, sprite1, sprite2);

If you have many sprites, the best way is to copy the sprite indexes into an array and then pull one out using a randomly generated array index based on the highest index of the array.

rand_sprite[1] = sprite0;
rand_sprite[2] = sprite1;
rand_sprite[3] = sprite2;
rand_sprite[4] = sprite3;

random_index = floor(random(4)) + 1;
random_sprite = rand_sprite[random_index];

Once you have selected a random sprite index, you can draw it using draw_sprite()

draw_sprite(random_sprite,0,100,100);

or show it by assigning it as the current sprite for an instance.

sprite_index = random_sprite;

Color

The color of a sprite can be changed with the built-in variable image_blend. [pro]

Transformation

Rotation

All sprite rotation is handled with the variable image_alpha. [pro] Using image_alpha a sprite can be rotated, spun, turned and pointed.

Naming Conventions

Sprites are usually prefixed with spr to distinguish them from the object that they represent. Examples :

  • spr_player
  • sprPlayer

Related Pages

Reference

GML

Functions
Variables

Local Variables

These local variables apply only to the current sprite of an instance.

Sprite ID

Sprite Dimensions

Animation

  • image_speed - the animation speed of a sprite. You can use it to speed up or slow down the animation.

Reversing Animation (playing animation backwards)

To reverse the animation of a sprite, set the image_speed to a negative number:

image_speed = -.5;

Scaling

  • image_xscale - used to shrink or expand a sprite horizontally
  • image_yscale - used to shrink or expand a sprite vertically

Effects

Functions

FAQs

Sprites as Resources

Tutorials

Links

Game Maker Elements: Backgrounds | Fonts | Objects | Paths | Rooms | Scripts | Sounds | Sprites | Time Lines
Categories: Game Maker : Graphics : Sprites