Platformer antimoonwalking is actually pretty easy. This may seem long, but it is the same logic for all of the scripts. I just put them all there. I'll elaborate on the way jimmynewguy showed you. Since you have two possible directions, you can use a variable called direction. Upon keydown left, set it to 0, upon keydown right, set it to 1. Upon those keydowns, you must switch the animation, depending on if left, right, or both keys are pressed.
KeyDown(Right)[Disable repeat]->Script Editor
- Code: Select all
char* key=GetKeyState(); // Allows keyboard interaction (key[KEY_<your key here>]) is either 1 or 0
if (canjump==1) // if actor is on the ground
{
if (key[KEY_LEFT] == 0 && key[KEY_RIGHT] == 1) // If only right is pressed (allow for walking, so change animation)
{
ChangeAnimation("Event Actor", "WalkRight", FORWARD); // Because only right is pressed, change to walking
}
else if (key[KEY_LEFT] == 1 && key[KEY_RIGHT] == 1) // If both right and left (don't allow for walking, stand animation)
{
ChangeAnimation("Event Actor", "Right", FORWARD); // Because both are pressed
}
}
right=1; // right
KeyDown(Left)[Disable repeat]->Script Editor
- Code: Select all
char* key=GetKeyState();
if (canjump==1)
{
if (key[KEY_LEFT] == 1 && key[KEY_RIGHT] == 0)
{
ChangeAnimation("Event Actor", "WalkLeft", FORWARD);
}
else if (key[KEY_LEFT] == 1 && key[KEY_RIGHT] == 1)
{
ChangeAnimation("Event Actor", "Left", FORWARD);
}
}
direction=0; // left
Right and left are to determine jump animations, or any other that you may put in. The reasoning for this is you need to make sure that the actor knows which way it's facing. Since the event is not repeated, the last keydown will define the direction.
Now for the keyup. These follow almost identical logic for sorting which direction to go as above. The only difference is due to the nature of keyup. Since that is an event triggered by a switch from 1 to 0, the conditions will change. If left is still pressed than walk. If both are released set it to stand.
KeyUp(Right)->Script Editor
- Code: Select all
char* key=GetKeyState();
if (canjump==1)
{
if (key[KEY_LEFT] == 1 && key[KEY_RIGHT] == 0) // If left is still pressed, walk left
{
ChangeAnimation("Event Actor", "WalkLeft", FORWARD);
}
else if (key[KEY_LEFT] == 0 && key[KEY_RIGHT] ==0) // As left and right are not pressed, set it to stand
{
ChangeAnimation)"Event Actor", "Right", FORWARD);
}
}
KeyUp(Left)->Script Editor
- Code: Select all
char* key=GetKeyState();
if (canjump==1)
{
if (key[KEY_LEFT] == 0 && key[KEY_RIGHT] == 1)
{
ChangeAnimation("Event Actor", "WalkRight", FORWARD);
}
else if (key[KEY_LEFT] == 0 && key[KEY_RIGHT] == 0)
{
ChangeAnimation)"Event Actor", "Left", FORWARD);
}
}
The last bit is in the draw actor. This will set your movement, only if a single key is pressed.
DrawActor->Script Editor
- Code: Select all
char* key=GetKeyState();
if (key[KEY_LEFT] == 1 && key[KEY_RIGHT] == 0) // Only left
{
x- = 2; // Move left
}
if (key[KEY_LEFT] == 0 && key[KEY_RIGHT] == 1) // Only right
{
x+= 2; // Move right
}
yvelocity+=9; // Because this is the draw actor script, nothing to do with moonwalking