Jump to content


Photo

Best way to animate diff states/sprite questions


  • Please log in to reply
2 replies to this topic

#1 Cognac

Cognac

    GMC Member

  • New Member
  • 15 posts
  • Version:Mac

Posted 23 February 2012 - 08:39 PM

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 :P

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

Edited by Cognac, 23 February 2012 - 08:41 PM.

  • 0

#2 RhysAndrews

RhysAndrews

    Game Designer

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

Posted 23 February 2012 - 09:57 PM

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 :P

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


  • 0

banner4.jpg


#3 Cognac

Cognac

    GMC Member

  • New Member
  • 15 posts
  • Version:Mac

Posted 24 February 2012 - 09:35 PM

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!

Edited by Cognac, 24 February 2012 - 11:28 PM.

  • 0