Skip to main content
March 28, 2014
Answered

Help on AS2 keyListener script

  • March 28, 2014
  • 1 reply
  • 424 views

Hi, I have been this ActionScript a lot -

var keyListener:Object = new Object();

Key.addListener(keyListener);

function keyDownF(){

    play();

}

keyListener.onKeyDown = keyDownF;

But would like to know how to revise it so I could isolate the specific keys I do or don't want to be used.

Also is it possible to disable so it only works at a particular part in my time line? If I use it now it will work on every frame.

Any advice would be helpful.

Thanks again

Jason

This topic has been closed for replies.
Correct answer Ned Murphy

Hi Jason, here is a simplified bit of code that works based on specific keys.  You would just replace the "trace()" commands with your intended actions...

var keyListener:Object = new Object();
Key.addListener(keyListener);

keyListener.onKeyDown = function(){


    // trace(Key.getCode()); // use this trace to determine other key codes


    if(Key.getCode() == 38) trace("up");
    if(Key.getCode() == 40) trace("down");
    if(Key.getCode() == 37) trace("left");
    if(Key.getCode() == 39) trace("right");
}

As far as only allowing this to function in specific frames, you could utilize other conditionals that check if the _currentframe property is a value that allows for it.  You could also assign the Key.onKeyDown to be null when you do not want it to be active.

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
March 28, 2014

Hi Jason, here is a simplified bit of code that works based on specific keys.  You would just replace the "trace()" commands with your intended actions...

var keyListener:Object = new Object();
Key.addListener(keyListener);

keyListener.onKeyDown = function(){


    // trace(Key.getCode()); // use this trace to determine other key codes


    if(Key.getCode() == 38) trace("up");
    if(Key.getCode() == 40) trace("down");
    if(Key.getCode() == 37) trace("left");
    if(Key.getCode() == 39) trace("right");
}

As far as only allowing this to function in specific frames, you could utilize other conditionals that check if the _currentframe property is a value that allows for it.  You could also assign the Key.onKeyDown to be null when you do not want it to be active.