我想制作一个菜单,当它不存在时,按 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";
}
}
}
答案 0 :(得分:3)
如果now
是TextField
,您需要比较其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
类。