在as3中禁用重复键盘按下事件
现在我试图让键盘事件停止重复。
我的想法是当按键被按下时有一个真假条件,这样如果按键已经按下,它就不会重复。
//Mouse Event Over
keyCButton.addEventListener(MouseEvent.MOUSE_OVER, function(){gotoAndStop(2)});
//Variable
var Qkey:uint = 81;
//Key Down Event
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
var soundplayed = false;
function keydown(event:KeyboardEvent){
if (event.keyCode==Qkey) {
this.soundplayed=true;
}
}
if (this.soundplayed==false){
gotoAndPlay(3);
}
//Key Up Event
stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
function keyup(event:KeyboardEvent){
this.soundplayed=false;
gotoAndStop(1);
}
这样做会使按键一遍又一遍地循环,而无需键盘事件 我想我需要在“if (this.soundplayed==true)”中添加一个“&& keyDown...”,但我不知道如何在不出现错误的情况下做到这一点,
这是我正在尝试的键盘手修复http://soulseekrecords.org/psysci/animation/piano.html
now I'm trying to make the keyboard events to stop repeating.
My idea was to have a true and false condition for when the key is pressed so that it wont repeat if the key is down already.
//Mouse Event Over
keyCButton.addEventListener(MouseEvent.MOUSE_OVER, function(){gotoAndStop(2)});
//Variable
var Qkey:uint = 81;
//Key Down Event
stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
var soundplayed = false;
function keydown(event:KeyboardEvent){
if (event.keyCode==Qkey) {
this.soundplayed=true;
}
}
if (this.soundplayed==false){
gotoAndPlay(3);
}
//Key Up Event
stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
function keyup(event:KeyboardEvent){
this.soundplayed=false;
gotoAndStop(1);
}
doing this makes the key loop over and over without a keyboard event
I think i need to add a "&& keyDown..." to "if (this.soundplayed==true)" but i dont know how to do it without getting errors
here is the keyboard player i'm trying to fix http://soulseekrecords.org/psysci/animation/piano.html
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是 Kishi 已经建议的另一种(可能更通用)的编写方式:
将调用 processKeyDown 函数,就像禁用了 keydown 重复一样。如果您需要在按键按下时执行某些操作,请将该代码放入 keyUp 函数中,或者调用像 processKeyDown 一样定义的 processKeyUp 函数。
Just another (perhaps a bit more general) way to write what Kishi already suggested:
The processKeyDown function will be called as if keydown repeating was disabled. If you need to do something when the key is up, put that code in the keyUp function, or maybe call a processKeyUp function defined like processKeyDown.
我不确定你在这些框架上做什么。
这是完整的代码吗?
不管怎样,你应该尝试这样的事情:
注意 keydown 事件监听器现在将执行一次 - 我的意思是..至少 if 分支 - 作为 soundplayed 变量用作锁定机构。
它只会在执行keyup后再次执行(this.soundplayed = false)。
I'm not sure what you are doing on these frames..
Is that the complete code?
Anyway, you should try something like this:
Notice that the keydown event listener will now execute once -- I mean.. at least the if branch -- as the soundplayed variable is used as a locking mechanism.
It'll only execute again after keyup's been executed (this.soundplayed = false).