wuiyang
wuiyang

Reputation: 429

AS3 the on/off menu wont work

I wanted to make a menu, when it was not there, press Esc to open it, when it was there, press Esc to close it. but this wont work, it show this error:

1176: Comparison between a value with static type flash.text:TextField and a possibly unrelated type String.

This is my code

stage.addEventListener(KeyboardEvent.KEY_DOWN, down);
function down(keyEvent:KeyboardEvent):void
{
    var keyPressed:String = "";
    keyPressed = keyEvent.keyCode.toString();
    if (keyPressed == "27")
    {
        if (now == "0")
        {
            menu._x = 100;
            now.text = "1";
        }
        else if (now == "1")
        {
            menu._x = -400;
            now.text = "0";
        }
    }
}

Upvotes: 0

Views: 74

Answers (1)

Barış Uşaklı
Barış Uşaklı

Reputation: 13532

If now is a TextField you need to compare its text property

stage.addEventListener(KeyboardEvent.KEY_DOWN, down);
function down(keyEvent:KeyboardEvent):void
{
    if (keyEvent.keyCode == Keyboard.ESCAPE)
    {
        if (now.text == "0")
        {
            menu._x = 100;
            now.text = "1";
        }
        else if (now.text == "1")
        {
            menu._x = -400;
            now.text = "0";
        }
    }
}

You can also set visible to false/true to hide show the menu instead of moving it off the stage. I also changed the keyCode to use the Keyboard class.

Upvotes: 3

Related Questions