First, create a new game. Don't try integrating this into your game until you understand how it works. Now, create an actor, name him what ever you like. Give him the caveman graphics for this example. ALl 4 animations, charLeft,Right,StopLeft,StopRight. Now add a draw actor and type this
- Code: Select all
char*key=GetKeyState():
This will get the key state so you can use the KeyDown events in Draw Actor. Now, create an int using code like this
- Code: Select all
int DIR=key[KEY_LEFT]-key[KEY_RIGHT];
This means, when key LEFT is pressed, the value of DIR is 1. When key RIGHT is pressed, the value of DIR is -1. When none or both are pressed, its 0. Now, make a switch statement like this:
- Code: Select all
switch(DIR) {
}
Now inbetween that, create "cases". First create this one:
- Code: Select all
case -1:
x+=5;
ChangeAnimation("Event Actor", "charRight", NO_CHANGE);
break;
Now this means if the player is pressing right, he'll go right and change his animation to right. Now add another one after the "break". Break means it ends off that case. Anyway, add this under that.
- Code: Select all
case 1:
x-=5;
ChangeAnimation("Event Actor", "charLeft", NO_CHANGE);
break;
This means if left is pressed, he'll go left and walk left. Finally, add a case 0, which means if none or both are pressed, like this
- Code: Select all
case 0:
if(animindex==getAnimIndex("charRight")) {
ChangeAnimation("Event Actor", "charStopRight", NO_CHANGE);
}
else if(animindex==getAnimIndex("charLeft")) {
ChangeAnimation("Event Actor", "charStopLeft", NO_CHANGE);
}
break;
the part that says if (animindex) means if the animation is Equal to charRight, then it changes to StopRight. Else, it changes to stop Left. Your final code should look like this
- Code: Select all
char*key=GetKeyState();
int DIR=key[KEY_LEFT]-key[KEY_RIGHT];
switch(DIR) {
case -1:
x+=5;
ChangeAnimation("Event Actor", "charRight", NO_CHANGE);
break;
case 1:
x-=5;
ChangeAnimation("Event Actor", "charLeft", NO_CHANGE);
break;
case 0:
if(animindex==getAnimIndex("charRight")) {
ChangeAnimation("Event Actor", "charStopRight", NO_CHANGE);
}
else if(animindex==getAnimIndex("charLeft")) {
ChangeAnimation("Event Actor", "charStopLeft", NO_CHANGE);
}
break;
}
Hope this helps
Skydereign: if you know of another way to get the animindex without using if, please let me know.
Special thanks to Sky and other programmers of GE.