Jump to content


Photo

Best practices for sprite animation.


  • Please log in to reply
7 replies to this topic

#1 hexdump

hexdump

    GMC Member

  • GMC Member
  • 360 posts
  • Version:GM:Studio

Posted 14 May 2012 - 11:18 AM

Hi,

I have started today to add animation to my sprites. The way people ussually do this is just creating a sprite per animatoin and then changing sprite_index? I have seen some code around to allow having a sprite with all frames and build animations from there (changing image_index, etc... at runtime). Could anyone comment?

Thanks.
  • 0

#2 annihilator127

annihilator127

    GMC Member

  • GMC Member
  • 143 posts
  • Version:GM8

Posted 14 May 2012 - 12:32 PM

I myself use the separate sprite method. I think it's neater because you don't have to worry about managing different image_index domains; Game Maker does it for you. It works like this:

If gravity is not equal to 0 (it gets set to 0 when George is on the ground)
change the sprite to spr_George_jump with subimage 0 and speed 0.25
Else
If hspeed is not equal to 0,
change the sprite to spr_George_walkwith subimage 0 and speed 0.25
Else
change the sprite to spr_George_stand with subimage 0 and speed 0.25
Then I play with image_xscale to change the facing direction.

If you put all the animations in a single sprite, you have to put code something like this in an Posted Image End Step event:
if (image_index == 6) image_index == 1;
if (image_index == 12) image_index == 7;
if (image_index == 18) image_index == 13;
if (image_index == 24) image_index == 0;
Because there's only one sprite (with a lot of subimages), it makes the resource list more compact, but I don't like this because 1) you can sort the sprites in groups anyway and 2) you lose the ability to label the different animations.

Edited by annihilator127, 14 May 2012 - 12:33 PM.

  • 0

#3 Arkaic.ind

Arkaic.ind

    GMC Member

  • GMC Member
  • 76 posts
  • Version:GM8

Posted 14 May 2012 - 01:01 PM

^^ Agreed.
  • 0

#4 hexdump

hexdump

    GMC Member

  • GMC Member
  • 360 posts
  • Version:GM:Studio

Posted 14 May 2012 - 01:50 PM

I myself use the separate sprite method. I think it's neater because you don't have to worry about managing different image_index domains; Game Maker does it for you. It works like this:

If gravity is not equal to 0 (it gets set to 0 when George is on the ground)
change the sprite to spr_George_jump with subimage 0 and speed 0.25
Else
If hspeed is not equal to 0,
change the sprite to spr_George_walkwith subimage 0 and speed 0.25
Else
change the sprite to spr_George_stand with subimage 0 and speed 0.25
Then I play with image_xscale to change the facing direction.

If you put all the animations in a single sprite, you have to put code something like this in an Posted Image End Step event:

if (image_index == 6) image_index == 1;
if (image_index == 12) image_index == 7;
if (image_index == 18) image_index == 13;
if (image_index == 24) image_index == 0;
Because there's only one sprite (with a lot of subimages), it makes the resource list more compact, but I don't like this because 1) you can sort the sprites in groups anyway and 2) you lose the ability to label the different animations.


Yup, I got same feeling. Just wanted to know what people were using :).

Thanks for the info.

Edited by hexdump, 14 May 2012 - 01:51 PM.

  • 0

#5 kburkhart84

kburkhart84

    GMC Member

  • GMC Member
  • 1608 posts
  • Version:GM:Studio

Posted 14 May 2012 - 03:34 PM

Agreed with the above posters as well.

Also with this "normal" method, you can use the animation_end event to change sprites automatically at the end of the animation.
  • 0

#6 hexdump

hexdump

    GMC Member

  • GMC Member
  • 360 posts
  • Version:GM:Studio

Posted 14 May 2012 - 08:09 PM

Agreed with the above posters as well.

Also with this "normal" method, you can use the animation_end event to change sprites automatically at the end of the animation.


Thanks mate.
  • 0

#7 Nocturne

Nocturne

    Nocturne Games

  • Administrators
  • 16793 posts
  • Version:GM:Studio

Posted 14 May 2012 - 08:15 PM

You should be careful with the method posted above due to the fact that GameMaker deals with floats. On a PC or Mac this is less of a problem, but with HTML5 and devices it is much more so... So the code above should really read...

if (image_index > 6 - image_speed) image_index == 1;
if (image_index > 12 - image_speed) image_index == 7;
//etc...

That will take care of any precision errors. I should also add, that all that can be avoided by simply making your sprites into separate ones and changing sprite index, as there is honestly no real reason not to! In many cases doing anything else is just complicating your life for the hell of it...
  • 0

#8 Arkaic.ind

Arkaic.ind

    GMC Member

  • GMC Member
  • 76 posts
  • Version:GM8

Posted 15 May 2012 - 01:06 PM

haha go Nocturne :)
  • 0




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users