如何在Action Script中禁用键盘键?
我正在制作Flash“记忆”游戏,想法发现2张相同的牌。当发现第二张卡时,显示750毫秒,此时玩家无法执行任何操作。
当我使用此mouseChildren = false;
播放器时,此时无法用鼠标点击,但他可以使用键盘箭头/输入/空格/制表符按钮...我需要暂时禁用它。
以下是我的代码的一部分:
{
trace("Wrong");
_message = "Wrong";
message_txt.text = _message;
_secondCard = event.currentTarget;
var timer:Timer = new Timer(750, 1);
timer.addEventListener(TimerEvent.TIMER_COMPLETE, flipBack);
timer.start();
stage.addEventListener(KeyboardEvent.KEY_DOWN, blindKeyboard);//added here
stage.addEventListener(KeyboardEvent.KEY_UP, blindKeyboard);//added here
mouseChildren = false;
}
}
function blindKeyboard(e:KeyboardEvent):void{ //added here function
e.preventDefault();
e.stopPropagation();
}
protected function flipBack(event:TimerEvent):void
{
_firstCard.gotoAndPlay("flipBack");
_secondCard.gotoAndPlay("flipBack");
_firstCard.addEventListener(MouseEvent.CLICK, checkCards);
_secondCard.addEventListener(MouseEvent.CLICK, checkCards);
_firstCard = _secondCard = undefined;
mouseChildren = true;
}
答案 0 :(得分:0)
尝试
stage.addEventListener(KeyboardEvent.KEY_DOWN, blindKeyboard);
stage.addEventListener(KeyboardEvent.KEY_UP, blindKeyboard);
function blindKeyboard(e:KeyboardEvent):void{
e.preventDefault();
e.stopPropagation();
}
答案 1 :(得分:0)
您可以使用添加/删除侦听器的功能:
function addListeners():void
{
// loop through and add the listeners for the cards
// add keyboard listeners
}
function removeListeners():void
{
// loop through and remove listeners from the cards
// remove keyboard listeners
}
在设置计时器之前,您将删除听众:
removeListeners();
然后在您的flipback计时器处理程序中,您只需调用addListeners:
addListeners();