AS3开/关菜单不起作用

时间:2013-04-26 15:07:38

标签: actionscript-3

我想制作一个菜单,当它不存在时,按 Esc 打开它,当它在那里时,按 Esc 关闭它。但是这不起作用,它显示了这个错误:

1176:静态类型flash.text:TextField的值与可能不相关的类型String之间的比较。

这是我的代码

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";
        }
    }
}

1 个答案:

答案 0 :(得分:3)

如果nowTextField,您需要比较其text属性

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";
        }
    }
}

您还可以将visible设置为false/true以隐藏显示菜单,而不是将其移出舞台。我还更改了keyCode以使用Keyboard类。