在as3中禁用重复键盘按下事件

发布于 2024-08-30 18:43:43 字数 934 浏览 7 评论 0原文

现在我试图让键盘事件停止重复。

我的想法是当按键被按下时有一个真假条件,这样如果按键已经按下,它就不会重复。

//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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

眼泪也成诗 2024-09-06 18:43:43

这是 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 函数。

Just another (perhaps a bit more general) way to write what Kishi already suggested:

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);
}

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.

夢归不見 2024-09-06 18:43:43

我不确定你在这些框架上做什么。
这是完整的代码吗?

不管怎样,你应该尝试这样的事情:

// 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)。

I'm not sure what you are doing on these frames..
Is that the complete code?

Anyway, you should try something like this:

// 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);
}

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).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文