Skip to main content
Participant
February 13, 2013
Answered

Configuring a key to execute a button

  • February 13, 2013
  • 1 reply
  • 622 views

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!

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

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

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
February 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

Participant
February 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?

Ned Murphy
Legend
February 14, 2013

Try:  delete keyListener.onKeyDown;