• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

GML Animations for platformers - all in one

Bluetail7

Member
GM Version: Game Maker Studio 1
Target Platform
: ALL
Download: N/A
Links: N/A

Summary:
In this tutorial I will explain the basic things you need to know to make animation smooth and easy to read.
Using a single sprite with many subimages

Tutorial:
Greetings everyone! In this tutorial I will tell you some basic rules when coding in GMS
  • Don't loop any function that affects your visuals: sprite_index, image_index (if not using relatives)
  • Chain the sprite with the character's movement, then keys pressed, not the opposite!
  • Always check if the animation isn't running, so you won't make it look awkward/interrupt when pressing that button again.
  • Check code's position!! It matters a lot as you may soft lock the animation itself.
  • Just a preference: create variables for things that the character rarely does: combos, automatic movements that block some user input, so you can use it in the animation core.
  • Another preference: use animations outside core gameplay.
In which events can I use my sprite functions?
  • Create event
  • Step event
  • Key pressed (often for quick time events)
  • Key press (recommended for most animations)
  • Key released (sometimes used to throw or shoot bullets)
  • Animation end
The easiest place to use them are in the step event.

Now into the code itself

lets say we have a sprite with the following
Code:
frame 0 = idle
frames 1-3 = walk cycle
frame 4 = start jump
frame 5 = middle jump
frame 6 = falling
frames 7-10 = sword attack 1
frames 10-15 = sword atk 2
frames 15-23 = sword atk 3
Now to the action!

first: character's properties (using some basic movement functions)
Code:
functions:
hspeed
vspeed
image_index
image_speed

variables:
combo
can_jump
2nd: the code itself
Code:
//STEP EVENT: Gameplay
if (keypressed) //ATTACK
   {
      if (canjump)
         {
            if (img_index >= 9 && img_index <= 10 || img_index >= 13 && img_index <= 15)
               {if (keypressed) {combo++;}}
            else
            if (!combo) //Not in combo mode
               {
                  if (keypressed && combo < 3)
                     {
                        //change index to skip to the first frame
                        combo++;
                        img_speed = 0.5;
                        img_index = 7;
                     }
               }
         }
   }

if (keypressed) //JUMP
   {
      if (canjump && !combo)
         {
            vspeed = -8;
            can_jump--;
            img_speed = 0;
         }
   }

Code:
//STEP EVENT: Animation core
if (can_jump) //is on ground
   {
      //COMBO
      if (combo > 0)
         {
            //you can use a switch too
            if (combo == 3) {if (img_index == 23) {combo = 0;}}
            else
            if (combo == 2) {if (img_index == 15) {combo = 0;}}
            else
            if (combo) {if (img_index == 10) {combo = 0;}}

            if (vspeed!=0) {combo = 0;} //stop combo if in mid air
         }
      else
      //IDLE
      if (hspeed == 0) {img_index=0; img_speed = 0;}
      else
      //WALK
         {
            if (img_index < 1 || img_index > 3) {img_index = 1;}
            image_speed = 0.5;
         }
   }
else
//IS JUMPING
   {
      //this is what I normally do with 3 frames jump
      img_index = Middle jump + sign(vspeed);
      combo = 0; //just in case you fall from an edge
   }
 
Last edited:
Top