Choose animation frames groups for a same object

Axl Trauts


I don´t quite understand if I can have different "animation groups" for a same object depending on the condition of that object, I mean, what is the object doing.

For example: I have a top-down shark object which will start swimming underwater. When is near the player, say 100px away, it will emerge, wait a bit and swim at 2x speed downwards.

The underwater animation is 2 frames (loop), the emerging would be 2 frames (don't loop) and the emerged swim is 2 frames (loop).

I usually create as many sprites as animation groups I need and then draw on the same object depending on the "if" condition. In this case I created 3 sprites for each instance and draw each one on the object obj_shark depending on the situation. I think it's not the best way, I'd like to know if that is the case and how to improve it.


Indeed, I find it best to have each looped animation as a sepparate sprite, like you're doing. Then I just assign the sprite_index = <desired sprite>.

If you have many types of creatures doing the same thing you may want to assign them a macro so you can set animations in arrays by type, and then use the macro to select the corresponding one, so you have one code that handles all of them.
For example on the control object:
#macro SHARK     0
#macro WHALE     1
#macro DOLPHIN   2
#macro SUBMARINE 3

sprite_enemies_swimfar = [spr_shark_swimfar, spr_whale_swimfar, spr_dolphin_swimfar, spr_sub_swimfar];
sprite_enemies_swimclose = [spr_shark_swimclose, spr_whale_swimclose, spr_dolphin_swimclose, spr_sub_swimclose];
then later on the enemy:
mytype = SHARK;
sprite_index = control_object.sprite_enemies_swimfar[mytype];