流畅地处理多个关键presses中的ActionScript3 [英] fluidly dealing with multiple keypresses in ActionScript3

查看:107
本文介绍了流畅地处理多个关键presses中的ActionScript3的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在建设一个发动机一个成熟的游戏的那一刻,但我注意到,ActionScript3的是保持流体有困难时,多键presses都在使用。例如;

I'm currently building an engine for a platformer game at the moment, but I've noticed that ActionScript3 is having difficulty in keeping fluid when multiple keypresses are in use. For example;

        function onKeyDown(event:KeyboardEvent):void {
        if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.W || event.keyCode == Keyboard.SPACE) {
            if (isTouchingGround()) {
                isJumping = true;
                yv = -100;
            }
        } else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.D) {
            if (xv == 0) {
                player.gotoAndPlay(275);
            }
        } else if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.A) {
            if (xv == 0) {
                xv = -24;
            } else if (xv != -120) {
                xv-=2;
            }
        } else if (event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.D) {
            if (xv == 0) {
                xv = 24;
            } else if (xv != 120) {
                xv+=2;
            }
        }
    }

所以,上面列出,使用UP(或W或空格键)触发玩家跳跃(单独onframe事件处理程序处理重力等)。使用正确的(或D ..)键触发增加了播放器加速,这再次施加给玩家在一个单独的onframe事件处理程序。

So, as listed above, using the UP (or W, or Space) key triggers the player to jump (seperate onframe event handler handles gravity etc). Using the RIGHT (or D..) key triggers increases the player acceleration, which is again applied to the player in a seperate onframe event handler.

一切正常,本身 - 但是当多个按键的使用问题就出现了。如果玩家开始移动到右侧,并命中跳跃,他将停止加速。同时,他也不会减速,按照指示在Keyboard.UP方法。相反,他将维持他目前的速率常数,直到RIGHT键再次袭来。

Everything works fine by itself - but the problem arises when multiple keystrokes are used. If a player starts to move to the right, and hits jump, he will cease accelerating. At the same time, he will not decelerate, as instructed in the Keyboard.UP method. Instead, he will maintain constant at his current rate, until the RIGHT key is hit again.

总之,这是因为,虽然动作脚本开始忽略这两个keyboard.down和keyboard.up方式为右或左移动键,直到它们不再被pressed。这显然​​会导致一些非常严格的游戏 - ?有没有什么解决办法有谁愿意在此与我分享

In short, it is as though Actionscript begins ignoring both the keyboard.down and keyboard.up methods for the RIGHT or LEFT movement keys, until they are no longer being pressed. This obviously causes for some very rigid gameplay - is there any solution anyone would be willing to share with me on this?

推荐答案

您的问题在于,你如果后面的if else条件语句条件句。掉落的东西,只是有,如果条件句。基本上,如果用户按住空格,则没有任何其他的,如果条件语句都将受到考验,因为空间被关押下来,它是第一个if语句。所以刚落别人掉的,如果的。只是删除如果正在测试按键,其他条件语句没有里面的条件语句中如果处理按键声明。

这是你的code应该是这样的:

Your problem lies in the fact that your if conditionals are followed by if else conditionals. Drop the else and just have the if conditionals. Basically if the user holds down space then none of the other if conditionals are going to be tested since space is being held down and it's the first if statement. So just drop the else off of the if's. Just remove the if else conditionals that are testing keystrokes, not the conditionals inside of the if statements that deal with keystrokes.

Here is what your code should look like:

function onKeyDown(event:KeyboardEvent):void {
    if (event.keyCode == Keyboard.UP || event.keyCode == Keyboard.W || event.keyCode == Keyboard.SPACE) {
        if (isTouchingGround()) {
            isJumping = true;
            yv = -100;
        }
    }
    if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.D) {
        if (xv == 0) {
            player.gotoAndPlay(275);
        }
    }
    if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.A) {
        if (xv == 0) {
            xv = -24;
        } else if (xv != -120) {
            xv-=2;
        }
    }
    if (event.keyCode == Keyboard.RIGHT || event.keyCode == Keyboard.D) {
        if (xv == 0) {
            xv = 24;
        } else if (xv != 120) {
            xv+=2;
        }
    }
}

别的东西,你可能会注意到的是,当向上键和RIGHT键都被按住,电脑似乎停止了键盘输入,但是当W键和D键被按住,你仍然可以preSS其他密钥和计算机将注册其输入。在这个问题的答案是<一个href="http://gaming.stackexchange.com/questions/6669/how-do-i-remove-the-limit-on-pc-keyboard-button-$p$psses">here.

有关流体的一部分,而不是触发东西时,击键发生,最好是有一个布尔变量,如 KEYUP UP 持有一个真实的,如果该键一次或假的时候,关键是向上。然后有一个函数的onEnterFrame(事件:事件):无效{} 执行一个动作时, KEYUP 是真实的。像这样:

For the fluid part, instead of triggering something when a keystroke takes place, it is better to have a boolean variable such as keyUP or UP that holds a true if the key is down or false when the key is up. Then have a function onEnterFrame(event:Event):void {} that performs an action when keyUP is true. Like so:

import flash.events.*;

public class keyEvents extends MovieClip {
    private var keyRIGHT:Boolean = false;

    public function keyEvents():void
    {
        this.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        this.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
        this.addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }

    function onKeyDown(event:KeyboardEvent):void
    {
        if(event.keyCode == Keyboard.RIGHT) {
            this.keyRIGHT = true;
        }
    }

    function onKeyUp(event:KeyboardEvent):void
    {
        if(event.keyCode == Keyboard.RIGHT) {
            this.keyRIGHT = false;
        }
    }

    function onEnterFrame(event:Event):void
    {
        if(this.keyRIGHT) {
            // This code is executed while the RIGHT arrow key is down.
        }
    }
}


如果上述code没有工作,我觉得你的问题在​​于你的键盘,而不是它的破裂或任何东西,但它被做可能会被搞乱的东西了道路。

让我知道这并没有帮助,我会继续努力。


If the above code does not work I think that your problem lies with your keyboard, not that it's broken or anything but the way it was made might be messing things up.

Let me know if this didn't help and I'll continue trying.

这篇关于流畅地处理多个关键presses中的ActionScript3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆