External Exam Download Resources Web Applications Games Recycle Bin

animated sprites | gml



When using an animated sprite, the heights of the sprite from frame to frame may change slightly. Because of this it is suggested you use the collision mask from the simple sprite you started with:



You can switch sprites in code as follows. This is an example only. You must adapt the following code to fit your own sprite changes.

objPlayer Create

grav = 1;
horiz = 0;
vert = 0;
move_speed = 7;
jump_speed = -15;
frict = 0.9;

facing = 1; //1 = right, -1 = left

objPlayer Step

    // ... this code snippet has been deliberately shortened ...
    
    // modify the sprite_index on left or right movements:
    if (keyboard_check(key_for_right)){ horiz = move_speed; sprite_index = sprWalk; };
    if (keyboard_check(key_for_left)){ horiz = -move_speed; sprite_index = sprWalk; };
    if (keyboard_check(vk_nokey)){ horiz = horiz * frict; sprite_index = sprStill; };
    
    // ... this code snippet has been deliberately shortened ...

objPlayer Draw

if not (horiz ==  0) { facing = sign(horiz) };

draw_sprite_ext(
    sprite_index,
    image_index,
    x,
    y,
    facing, //instead of image_xscale
    image_yscale,
    image_angle,
    image_blend,
    image_alpha
);
Extension Activities
1. Create your own sprites or import sprite strips from the web.

2. Adjust your code to change sprites in-game depending on your actions.

3. The sprite changing directions will look terrible without centered origins...