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

Configuring a key to execute a button

New Here ,
Feb 13, 2013 Feb 13, 2013

Hi guys,

i'm pretty sure this is quite simple to do, but i'm very newbie in Flash yet...

I just wanna easy way to add a function integrated in my button script (in the timeline, not inside the button)

e.g.: When i press "right arrow", it'll execute the "btn_next" button ("play();")

at moment, my AS in the timeline is:

stop();

btn_next.onRelease = function(){

          play()

}

btn_recu.onRelease = function(){

          gotoAndStop(1)

};

i'm looking for something like:

stop();

btn_next.onRelease = function(){

    if(Key.isDown(Key.RIGHT)) {

          play();

    }

}

btn_recu.onRelease = function(){

    if(Key.isDown(Key.LEFT)) {

       gotoAndStop(1)

     }

};

This is obviously wrong, just an exemple!

TOPICS
ActionScript
589
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 , Feb 13, 2013 Feb 13, 2013

If you want the keyboard to have the same functionality as the buttons then you need to create a listener for the keyboard and check the key pressed...

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

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

Just replace the traces with your action code

Translate
LEGEND ,
Feb 13, 2013 Feb 13, 2013

If you want the keyboard to have the same functionality as the buttons then you need to create a listener for the keyboard and check the key pressed...

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

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

Just replace the traces with your action code

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
New Here ,
Feb 14, 2013 Feb 14, 2013

Thanks Ned, it works...but there's a problem

when i advance to the next frame, and go back to the frame with this keyListener script, it goes cumulative (execute the trace functions twice)

how can i disable it after the first time?

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
LEGEND ,
Feb 14, 2013 Feb 14, 2013
LATEST

Try:  delete keyListener.onKeyDown;

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