现在我正在尝试让键盘事件停止重复。
我的想法是在按下按键时有一个真假条件,这样如果按键已经按下它就不会重复。
//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 ...”,但我不知道怎么做而不会出错#/ p>
这是我正在尝试修复http://soulseekrecords.org/psysci/animation/piano.html
的键盘手答案 0 :(得分:3)
另一种(也许是更一般的)写出Kishi已经建议的方式:
stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDown);
stage.addEventListener(KeyboardEvent.KEY_UP,keyUp);
var downKeys:Dictionary = new Dictionary();
function keyDown(e:KeyboardEvent):void {
if(!downKeys[e.keyCode]) {
downKeys[e.keyCode] = true;
processKeyDown(e);
}
}
function keyUp(e:KeyboardEvent):void {
delete downKeys[e.keyCode];
}
function processKeyDown(e:KeyboardEvent):void {
trace(e.keyCode);
}
将调用processKeyDown函数,就像禁用了keydown重复一样。如果你需要在密钥启动时执行某些操作,请将该代码放在keyUp函数中,或者调用一个像processKeyDown一样定义的processKeyUp函数。
答案 1 :(得分:0)
我不确定你在这些镜架上做了什么.. 这是完整的代码吗?
无论如何,你应该尝试这样的事情:
// Mouse Events
this.keyCButton.addEventListener(MouseEvent.MOUSE_OVER, function():void{ gotoAndStop(2) });
// Variables
var Qkey:uint = 81;
var soundplayed = false;
// Keyboard events
this.stage.addEventListener(KeyboardEvent.KEY_DOWN, keydown);
this.stage.addEventListener(KeyboardEvent.KEY_UP, keyup);
// Event listeners
function keydown(event:KeyboardEvent){
if (event.keyCode == Qkey && !this.soundplayed) {
this.soundplayed = true;
this.gotoAndPlay(3);
}
}
function keyup(event:KeyboardEvent){
this.soundplayed = false;
this.gotoAndStop(1);
}
请注意, keydown 事件侦听器现在将执行一次 - 我的意思是..至少 if 分支 - 因为soundplayed变量用作锁定机制。 它只会在 keyup 执行后再次执行( this.soundplayed = false )。