Search code examples
flashbuttonactionscript-2

how to disable Flash as2 button keyPress "<Right>"


I am trying to disable a button keyPress Right command just after it was pressed but until now I didn't succeed.

On the button I have:

on(press, keyPress "<Right>" ){
    myFunction();
}

Inside 'myFunction' among a lot of stuff I have:

some_bt.enabled = false; //it is working fine with mouse action!

The thing is this works only when user hits 'some_bt' with mouse, but not when he hit Right arrow. In many cases some users spam the Right and this messes up my code.

Any clue?

Thanks a lot.


Solution

  • Oh wow, haven't seen this "on(press)" code since like 10 years :) You need to assign a function with the onRelease method. Not sure about the exact syntax in AS2 but it was something like:

    // add a function to be called on release
    some_bt.onRelease = myFunction;
    
    myFunction = function()
    {
       // do your stuff here
    
       // remove the onRelease callback
       some_bt.onRelease = null;
    }
    

    For the right key you might listen for all keyboard events and disable it when done:

    var keyListener:Object = new Object();
    keyListener.onKeyDown = function() 
    {
       if(Key.getCode() == Key.RIGHT)
       {
           myFunction();
       }
    };
    Key.addListener(keyListener);
    

    and inside your myFunction:

    Key.removeListener(keyListener);