Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Help on AS2 keyListener script

Guest
Mar 27, 2014 Mar 27, 2014

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

TOPICS
ActionScript
402
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

LEGEND , Mar 28, 2014 Mar 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");
}

A

...
Translate
LEGEND ,
Mar 28, 2014 Mar 28, 2014
LATEST

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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines