flashactionscriptactionscript-2flash-8

ActionScript is activating two buttons at once


I'm trying to make a game in flash 8 (actionscript 2). So there are two arrows on the screen.When you press "up arrow"(from keyboard) once, one of the arrows will hide. When you press "up arrow" second time, the other arrow will hide two.But with my code when i press "up arrow" both arrows hide.Is there any way i can prevent that.

var x;
var y;
var myListener:Object = new Object();
function onKeyDown() {
    if (Key.isDown(Key.UP)) {
        x = 1;
        btn1._visible = false;
        Key.removeListener(this);
    }
    Key.addListener(this);
    if (Key.isDown(Key.UP)) {
        y = 1;
        btn2._visible = false;
        Key.removeListener(this);
    }
}
Key.addListener(this);

There are "x" and "y" because i will use them later if one or both arrows are clicked.


Solution

  • If you want several arrows btn1, btn2... to disappear one after the other, you can do like that:

    var a:Array = [btn1, btn2];
    var l:Number = a.length;
    var n:Number = 0;
    
    var keyListener:Object = new Object();
    
    keyListener.onKeyDown = function():Void {
        if (Key.getCode() == Key.UP && n < l) {
            a[n]._visible = false;
            n++;
        }
    }
    Key.addListener(keyListener);
    

    Remark

    Your variables x and y are replaced by n which value is 1 if the first arrow is invisible and 2 if the second arrow is also invisible.