Back



 
Arcade Movement

What now?
Feb.23.2003 - 10:53 am

I am trying something a little different today. Gonna put together some mini-tutorials that I call scripts. Just simple little things to do in Ags. The first being how to create real arcade movement. As in when you push an arrow key the character moves and when you let go they stop.

Arcade Movement

Bring it on down!
Feb.23.2003 - 10:53 am - Revised Mar.07.2004

These shouldn't get longer then a few paragraphs so if I ramble beat me. I will only show how to make Up, Down, Left and Right...but the ideas can be applied to corner keys pretty easily. First open your Global Script (Ctrl+G). Scroll down to the on_key_press function which will have a list of a bunch of if(keycode==blah) blah blah...so on and so forth. Just to try a little shock therapy I'm just gonna give the script in whole...

if (keycode==372) { // up arrow
    MoveCharacterStraight(EGO, player.x, player.y-200);
    while ((IsKeyPressed(372)>0) && (character[EGO].walking)) Wait(1);
    StopMoving(EGO);
}

if (keycode==375) { // left arrow
    MoveCharacterStraight(EGO, player.x-200, player.y);
    while ((IsKeyPressed(375)>0) && (character[EGO].walking)) Wait(1);
    StopMoving(EGO);
}

if (keycode==377) { // right arrow
    MoveCharacterStraight(EGO, player.x+200, player.y);
    while ((IsKeyPressed(377)>0) && (character[EGO].walking)) Wait(1);
    StopMoving(EGO);
}

if (keycode==380) { // down arrow
    MoveCharacterStraight(EGO, player.x, player.y+200);
    while ((IsKeyPressed(380)>0) && (character[EGO].walking)) Wait(1);
    StopMoving(EGO);
}

So there it is. Up, Left, Right and Down...Goodbye!....... Okay I'll explain. The first line:

if (keycode==372) { // up arrow

Checks if the Up arrow is being pushed...whoop! The second:

MoveCharacterStraight(EGO, player.x, player.y-200);

Moves the character directly up. It is important to use MoveCharacterStraight so that the character ignores walkable areas and only moves in a straight path. If not people would think your character is drunk. The third:

while ((IsKeyPressed(372)>0) && (character[EGO].walking)) Wait(1);

This is the important line. This checks if the key is still being pressed and if the character is still moving...If it isn't on to line 4:
StopMoving(EGO);

The character is stopped. Then the final brace ( '}' ) and the script is done. notice the minor changes for each key to accomodate the different keys to be pushed and different directions used...


Problems?

Double check that you placed the code inside the on_key_press function.

You may have changed your Characters Script name...The script above uses the default (EGO) go to the characters section in AGS and make sure you know the characters script name...if it is different change the script as needed.

If the character walks choppy (Which it shouldn't) let me know...I have a seperate script avaialable that uses the Repeadetly Execute function that will take care of it.

Any other problems please let me know.