1

如何在动作脚本中禁用键盘键?

我正在创建 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;
    }
4

2 回答 2

0

尝试

stage.addEventListener(KeyboardEvent.KEY_DOWN, blindKeyboard);
stage.addEventListener(KeyboardEvent.KEY_UP, blindKeyboard);
function blindKeyboard(e:KeyboardEvent):void{
    e.preventDefault();
    e.stopPropagation();
}
于 2013-05-04T15:33:33.710 回答
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();

然后在您的翻转计时器处理程序中,您只需调用 addListeners :

addListeners();
于 2013-05-04T18:52:39.317 回答