Generic Animation Code

Today I’ve written a function for general animation!  Its pretty generic and more animations can easily be added by adding states like swim, fly, or run and more directions like diagonals.  The variables face and state need to be initialize to something, I used ‘down’ and ‘stand’.  frame_speed can be hard-coded or an argument.  If your not using GML, you can probably still use it by defining xprevious and yprevious yourself (should be straight forward).  Making image_speed work without GML is more of a pain, I’ll post some code below which should be relatively portable to whatever language you use.

//set direction
if x < xprevious then face = ‘left’
if x > xprevious then face = ‘right’
if y < yprevious then face = ‘up’
if y > yprevious then face = ‘down’

//set walking/running more states can be added rather easily
if x!=xprevious and y!=yprevious then state = ‘walk’
else state = ‘stand’

//set animation settings
if face = ‘down'{
image_xscale = 1
sprite_index = spr_player_down}
else if face = ‘up'{
image_xscale = 1
sprite_index = spr_player_up}
else if face = ‘left'{
image_xscale = -1
sprite_index = spr_player_hori}
else if face = ‘right'{
image_xscale = 1
sprite_index = spr_player_hori}

//set set state settings
if state = ‘walk'{
image_speed = frame_speed}
else if state = ‘stand'{
image_speed = 0
image_index = 0}

ANIMATION WITHOUT GML

to make the above code work, you’ll need something like the following code.  Note, it will change a lot depending on the language, library and/or engine you use.

//this code is designed for an event based, object oriented game which includes a create(initialize), events are not required though.
//define number of sub images, this will likely go in your sprite objects initialization unless an engine handles it for you, in this example i called my sprite “sprite_name”
//the sprite object will generally contain an array of your sub images, i called this array image_list, a list is fine as your animation sub images are generally immutable
image_num = length(image_list);

//in your create event, initialize count in your object to be animated and animation speed(or define it later)
float image_count = 0;
float image_speed = 4/60; //in this case, this is 4 frames per second at 60 frames per second so 5 frames per second at 20 frames per second is 5/20
int image_index = 0;

//in your step event, set image_index
image_count += image_speed;
image_index = floor;
if (image_index > sprite_name.image_num){
image_count = 0;}

//in your draw event, will vary a lot depending on your engine or library
generic_drawing_function(x,y,sprite_index.image_list[image_index]) //or you function will generally take this form.

 

Advertisement

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s