Best way to animate diff states/sprite questions

Hello everyone, I am using GML on GM4MAC to make a platformer/action-rpg game.

1) What is the best way to animate the different states a character can be in (ex: standing, walking, attacking...)? Also, by states I don't mean the finite state machine, just states as in different situations the player can be in. I've been trying things out here and there and reading stuff, and right now I figure the best way to do it is to set up difference states (like if walking == true then...). If someone could expand on that idea I would appreciate it (for example, I've seen some games using movement=0 or movement=false, which one should I use and why? Where do I place the sprite_index variable? Should I start a game with standing=true then in the step event if standing==true{ sprite_index=spr_standing}?). If this isn't specific enough I'll explain further.

2) This one's been annoying me for a while, but should be fairly simple. How do I get an animation to play once in code? For instance, I want to press "Z" for an attack animation to play once. That's it

Anyway, if you're reading this thank you for your time! If I need to clear something up my pleasure!

Hi Cognac,There are many many ways to achieve it, and it's really down to the individual and also the engine of your game as to which method works best.

One method that I like to use is a big 'switch' for each character state. It seems among the most robust.Basically, create a 'constant' for each state (under the resources menu, I think). Call them something like state_standing or stateAttacking, whatever format you like, and make their value a unique number (starting from 0).

In the step event for your character, do something like this:

switch(myState)
{
case stateIdle:
if sprite_index!=sprIdle then sprite_index=sprIdle;
if keyboard_check_pressed(ord('Z'))
{
sprite_index=sprAttack;
image_index=0;
image_speed=.5;
myState=stateAttacking;
}
break;
case stateAttacking:
if sprite_index!=sprAttacking then sprite_index=sprAttacking;
//OR if sprite_index!=sprAttacking then myState=stateIdle;
if image_index>=image_number-1 then myState=stateIdle; //This returns the character to idle at the end of the animation
//You can add any other code here relating to attacking.. like checking for collisions with enemies, shooting bullets, etc
break;
}

Let me know if this makes no sense to you. The advantage of this method over say "if walking==true" is that you might get weird combinations such as "walking" AND "attacking" being true, and your code quickly becomes a huge mess. In this scenario, only ONE state can apply.

-Rhys

Hello everyone, I am using GML on GM4MAC to make a platformer/action-rpg game.

1) What is the best way to animate the different states a character can be in (ex: standing, walking, attacking...)? Also, by states I don't mean the finite state machine, just states as in different situations the player can be in. I've been trying things out here and there and reading stuff, and right now I figure the best way to do it is to set up difference states (like if walking == true then...). If someone could expand on that idea I would appreciate it (for example, I've seen some games using movement=0 or movement=false, which one should I use and why? Where do I place the sprite_index variable? Should I start a game with standing=true then in the step event if standing==true{ sprite_index=spr_standing}?). If this isn't specific enough I'll explain further.

2) This one's been annoying me for a while, but should be fairly simple. How do I get an animation to play once in code? For instance, I want to press "Z" for an attack animation to play once. That's it

Anyway, if you're reading this thank you for your time! If I need to clear something up my pleasure!

Thanks for the reply! This is exactly what I was looking for, I'll go test it out and then report it here. Thank you very much!

EDIT: It seems you can only add constants in the PRO version of Game Maker... That's too bad! I'd buy the pro version but GM4MAC has so many bugs that I'd feel cheated supporting this version (I'd go for pro GM on windows in a heartbeat but this isn't really an option for me). Hmm, I'll figure something out eventually!