| 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.
|
Table of Contents
|
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
- Collision
- Sprite Animation
- Sprite Collision
- Sprite Animation
- Sprite Color
- Sprite FAQ
- Sprite Properties Window
- Sprite Rotation
Reference
GML
Functions
Variables
Local Variables
These local variables apply only to the current sprite of an instance.
- depth
- image_alpha
- image_angle
- image_blend
- image_index
- image_number
- image_speed
- image_xscale
- image_yscale
- sprite_height
- sprite_index
- sprite_width
Sprite ID
- sprite_index - the number (or index) that refers to a sprite
- image_index - the number of the subimage displayed
Sprite Dimensions
- sprite_height - the height of a sprite
- sprite_width - the width of a sprite
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
- image_alpha pro - used to make a sprite transparent
- image_blend pro - used to make a sprite a shaded color
Functions
- draw_sprite() - draws a sprite
- draw_sprite_ext()
- draw_sprite_part() - can be used for a good-looking healthbar.
- draw_sprite_stretched()
FAQs
Sprites as Resources
- sprite_add()
- sprite_add_alpha()
- sprite_add_from_screen()
- sprite_add_from_surface()
- sprite_assign()
- sprite_create_from_screen()
- sprite_create_from_surface()
- sprite_delete()
- sprite_duplicate()
- sprite_merge()
- sprite_replace()
- sprite_replace_alpha()
- sprite_set_alpha_from_sprite()
- sprite_set_bbox()
- sprite_set_bbox_mode()
- sprite_set_offset()
- sprite_set_precise()
Tutorials
Links
| Game Maker Elements: Backgrounds | Fonts | Objects | Paths | Rooms | Scripts | Sounds | Sprites | Time Lines |
| Categories: Game Maker : Graphics : Sprites |