how do i make a movieclip move forever when an arrow key is pressed in flash actionscript 2.0? please answer

I'm working on a pac-man recreation in flash 8, using actionscript 2.0, and i need to makethe controls for pac-man, I want him to move forever when an arrow key is pressed, just like how he does in the original pac-man, so, can anyone give me the code for this?

I tried to find the code that makes pac-man move forever when an arrow key is pressed, but all i found is actionscript 3.0, i'm using actionscript 2.0

Upvotes: -1

Views: 43

Answers (1)

Organis
Organis

Reputation: 7316

If I remember correctly, there are two ways to do it.

First, you can subscribe to the relevant events and record the state of the keys, like that:

var UP:Boolean = False;
var DOWN:Boolean = False;

function onKeyUp()
{
    switch (Key.getCode())
    {
        case Key.UP:
            UP = False;
            break;
        case Key.DOWN:
            DOWN = False;
            break;
    }
}

function onKeyDown()
{
    switch (Key.getCode())
    {
        case Key.UP:
            UP = True;
            break;
        case Key.DOWN:
            DOWN = True;
            break;
    }
}

Key.addListener(this);

Then when there's time for Pacman to act, you can check them:

if (UP != DOWN)
{
    if (UP)
    {
        // Do what you do to move it up.
    }
    else // if (DOWN)
    {
        // Do what you do to move it down.
    }
}
else
{
    // If they are pressed at the same time or not pressed, do nothing.
}

The second way is to check if the keys are pressed on spot, when it is time for Pacman to perform an action:

var UP:Boolean = Key.isDown(Key.UP);
var DOWN:Boolean = Key.isDown(Key.DOWN);

if (UP != DOWN)
{
    if (UP)
    {
        // Do what you do to move it up.
    }
    else // if (DOWN)
    {
        // Do what you do to move it down.
    }
}
else
{
    // If they are pressed at the same time or not pressed, do nothing.
}

The difference between these two is that onKeyDown event is generated perpetually, like if you press a key and get a line of same characters in the text editor: aaaaaaaaaaaaaaaaaaa (but you cannot predict the stroke frequency and the difference between first interval and the rest, as it is controlled by the system), while Key.isDown(...) can be used on spot, if you meter your interactivity with timings, rather then bind it to keypress events.

Upvotes: 0

Related Questions