[英]AS3: Multiple options in one key and preventing multiple key presses
I have a "pause function" in my AS3 code, the problem is that i can't figure the logic necessary to give the letter "P" the capacity to pause and unpause and at the same time limit the number of presses to one at a time. 我的AS3代码中有一个“暂停功能”,问题是我无法弄清楚给字母“ P”提供暂停和取消暂停的能力所必需的逻辑,同时将按下次数限制为一个时间。 My code so far (working ,yes, but without the "one press at a time" limit). 到目前为止,我的代码(可以,是的,但是没有“一次按一下”的限制)。
public function PauseDown(event:KeyboardEvent):void
{
if (event.keyCode == Keyboard.P)
{
pause = true;
trace ("apreté pausa");
pausa();
}
}
public function pausa():void
{
trace ("pausa");
if (pause == true && paused == false)
{
paused = true;
backgroundLvL1.removeEventListener(Event.ENTER_FRAME,update);
}
else if(pause == true && paused == true)
{
paused = false;
backgroundLvL1.addEventListener(Event.ENTER_FRAME,update);
}
}
Your 'PauseDown' function should probably be called something else, because it's going to be called whenever ANY key is pressed; 您的“ PauseDown”函数可能应该调用其他名称,因为只要按下ANY键,就会调用该函数。 Something like 'keyPressed' is probably more descriptive. 像“ keyPressed”之类的东西可能更具描述性。
If you want to have only ONE key acknowledged as the most recently pressed key, I would set a variable (eg _currentKeyCode) to the event.keyCode in the 'keyPressed' function. 如果您只想将一个键确认为最近按下的键,则可以在“ keyPressed”功能的event.keyCode中设置一个变量(例如_currentKeyCode)。 Then, in your game loop function you can check the value of _currentKeyCode and have the game respond accordingly. 然后,在游戏循环功能中,您可以检查_currentKeyCode的值并使游戏做出相应的响应。
In the 'keyPressed' function you could also still include your test to see if 'P' has been pressed. 在“ keyPressed”功能中,您还可以包含测试以查看是否已按下“ P”。
Note that you should also have a 'keyReleased' function, triggered on a KeyBoardEvent.KEY_UP event. 请注意,您还应该具有在KeyBoardEvent.KEY_UP事件上触发的“ keyReleased”功能。 This could check if _currentKeyCode == the event.keyCode and, if so, set _currentKeyCode to -1. 这可以检查_currentKeyCode == event.keyCode,如果是,则将_currentKeyCode设置为-1。 Then, in your game loop, -1 would signify no key presses. 然后,在游戏循环中,-1表示没有按键。
One last, slightly irrelevant, thing: In the 'pausa' function you don't need to check if 'pause == true' because the pausa function is only called when pause == true. 最后,一点无关紧要的事情是:在“ pausa”函数中,您无需检查“ pause == true”是否成立,因为只有在pause == true时才调用pausa函数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.