To start off, we'll be using the caveman graphics. add the animations to an actor called "Player" exactly in this order:
- Code: Select all
charStopRight
charStopLeft
charRight
charLeft
your going to need to know how animindex works. animindex tells you the animation I.D, from 0 to however many animations you have on that actor. We have 4, so it starts from the top and goes down as it counts up. so like this:
- Code: Select all
charStopRight: 0
charStopLeft: 1
charRight: 2
charLeft: 3
You want to write down all of this on a notepad or gedit or what ever text editor you use. Specially if you have a lot of animations. Anyway, lets go into Events -- Draw Actor -- Script Editor. First thing we need to do is allow the key inputs
- Code: Select all
char*key=GetKeyState();
This will make it so the player receives keyboard inputs. Now add this command to create a variable called "dir"
- Code: Select all
int dir=key[KEY_RIGHT]-key[KEY_LEFT];
This means, that when keyRightArrow is pressed, dir=1, but when keyLeftArrow is pressed, it equals -1, and when none or both are pressed, it equals 0. Now lets make the player move when pressing those keys. using this code.
- Code: Select all
x+=(dir*5);
This will make the player move 5 pixels depending on the direction you press. Now lets add a switch statement to determine the animation changes to prevent the moonwalk bug
- Code: Select all
switch(dir)
{
case 0: //none or both keys are pressed;
switch(animindex)
{
case 2: //move right
ChangeAnimation("Event Actor", "charStopRight", FORWARD);
break;
case 3: //move left
ChangeAnimation("Event Actor", "charStopLeft", FORWARD);
break;
}
break;
case 1: //right arrow is pressed
ChangeAnimation("Event Actor", "charRight", NO_CHANGE);
break;
case -1: //left arrow is pressed
ChangeAnimation("Event Actor", "charLeft", NO_CHANGE);
break;
}
now play test the game Enjoy!
Full code to copy & paste:
- Code: Select all
char*key=GetKeyState();
int dir=key[KEY_RIGHT]-key[KEY_LEFT];
x+=(dir*5);
switch(dir)
{
case 0: //none;
switch(animindex)
{
case 2: //move right
ChangeAnimation("Event Actor", "charStopRight", FORWARD);
break;
case 3: //move left
ChangeAnimation("Event Actor", "charStopLeft", FORWARD);
break;
}
break;
case 1: //right
ChangeAnimation("Event Actor", "charRight", NO_CHANGE);
break;
case -1: //left
ChangeAnimation("Event Actor", "charLeft", NO_CHANGE);
break;
}