AS3 键盘控制

发布于 2024-11-02 15:11:06 字数 1647 浏览 8 评论 0原文

我有一个 as3 函数,可以用键盘控制影片剪辑:

包 {

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;

public class Main_Character_Two extends MovieClip
{
    var vx:int;
    var vy:int;

    public function Main_Character_Two()
    {
        init();
    }
    function init():void
    {
        //initialize variables
        vx = 0;
        vy = 0;

        //Add event listeners
        stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
    function onKeyDown(event:KeyboardEvent):void
    {
        if (event.keyCode == Keyboard.LEFT)
        {
            vx = -5;
        }
        else if (event.keyCode == Keyboard.RIGHT)
        {
            vx = 5;
        }
        else if (event.keyCode == Keyboard.UP)
        {
            vy = -5;
        }
        else if (event.keyCode == Keyboard.DOWN)
        {
            vy = 5;
        }
    }
    function onKeyUp(event:KeyboardEvent):void
    {
        if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
        {
            vx = 0;
        }
        else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP)
        {
            vy = 0;
        }
    }
    function onEnterFrame(event:Event):void
    {
        //Move the player
        player.x += vx;
        player.y += vy;
    }
}

}

这工作正常,但主要问题是,当您按下右键(并按住)然后按下左键时,角色将向左移动,但是当您释放左键时(仍然按住右键)向下)角色就停止了。在这种情况下,我怎样才能让角色开始再次向右移动(如果在释放左键后我仍然按住右键)

谢谢

I've got an as3 function that controls a movie clip with the keyboard:

package
{

import flash.display.MovieClip;
import flash.events.KeyboardEvent;
import flash.ui.Keyboard;
import flash.events.Event;

public class Main_Character_Two extends MovieClip
{
    var vx:int;
    var vy:int;

    public function Main_Character_Two()
    {
        init();
    }
    function init():void
    {
        //initialize variables
        vx = 0;
        vy = 0;

        //Add event listeners
        stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown);
        stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp);
        addEventListener(Event.ENTER_FRAME, onEnterFrame);
    }
    function onKeyDown(event:KeyboardEvent):void
    {
        if (event.keyCode == Keyboard.LEFT)
        {
            vx = -5;
        }
        else if (event.keyCode == Keyboard.RIGHT)
        {
            vx = 5;
        }
        else if (event.keyCode == Keyboard.UP)
        {
            vy = -5;
        }
        else if (event.keyCode == Keyboard.DOWN)
        {
            vy = 5;
        }
    }
    function onKeyUp(event:KeyboardEvent):void
    {
        if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
        {
            vx = 0;
        }
        else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP)
        {
            vy = 0;
        }
    }
    function onEnterFrame(event:Event):void
    {
        //Move the player
        player.x += vx;
        player.y += vy;
    }
}

}

This works ok but the main problem is that when you press the right key (and hold it down) then press the left key, the character will move to the left but when you release the left key (with the right key still held down) the character just stops. How can I make it so the character starts moving to the right again in this situation (if Im still holding the right key after Ive released the left key)

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

妄想挽回 2024-11-09 15:11:06

您可以将“玩家”定位代码放入 Event.ENTER_FRAME 循环中,并使用 KeyboardEvent.KEY_DOWNKeyboardEvent.KEY_UP 设置布尔值和位置循环中的播放器如下:

var leftIsPressed:Boolean = false;
var rightIsPressed:Boolean = false;
var upIsPressed:Boolean = false;
var downIsPressed:Boolean = false;
var speed:Number = 5;
var vx:Number = 0;
var vy:Number = 0;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

function keyDownHandler(e:KeyboardEvent):void {
    switch(e.keyCode) {
        case Keyboard.LEFT : leftIsPressed = true; break;
        case Keyboard.RIGHT : rightIsPressed = true; break;
        case Keyboard.UP : upIsPressed = true; break;
        case Keyboard.DOWN : downIsPressed = true; break;
    }
}

function keyUpHandler(e:KeyboardEvent):void {
    switch(e.keyCode) {
        case Keyboard.LEFT : leftIsPressed = false; break;
        case Keyboard.RIGHT : rightIsPressed = false; break;
        case Keyboard.UP : upIsPressed = false; break;
        case Keyboard.DOWN : downIsPressed = false; break;
    }
}

function enterFrameHandler(e:Event):void {
    vx = -int(leftIsPressed)*speed + int(rightIsPressed)*speed;
    vy = -int(upIsPressed)*speed + int(downIsPressed)*speed;
    player.x += vx;
    player.y += vy;
}

[编辑]添加了更详细的示例。

当然,在这种情况下,如果左右键都被按下,这将导致 0 偏移。

You could put your 'player' positioning code into an Event.ENTER_FRAME loop and use KeyboardEvent.KEY_DOWN and KeyboardEvent.KEY_UP to set booleans and position the player in the loop like:

var leftIsPressed:Boolean = false;
var rightIsPressed:Boolean = false;
var upIsPressed:Boolean = false;
var downIsPressed:Boolean = false;
var speed:Number = 5;
var vx:Number = 0;
var vy:Number = 0;

stage.addEventListener(KeyboardEvent.KEY_DOWN, keyDownHandler);
stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);

function keyDownHandler(e:KeyboardEvent):void {
    switch(e.keyCode) {
        case Keyboard.LEFT : leftIsPressed = true; break;
        case Keyboard.RIGHT : rightIsPressed = true; break;
        case Keyboard.UP : upIsPressed = true; break;
        case Keyboard.DOWN : downIsPressed = true; break;
    }
}

function keyUpHandler(e:KeyboardEvent):void {
    switch(e.keyCode) {
        case Keyboard.LEFT : leftIsPressed = false; break;
        case Keyboard.RIGHT : rightIsPressed = false; break;
        case Keyboard.UP : upIsPressed = false; break;
        case Keyboard.DOWN : downIsPressed = false; break;
    }
}

function enterFrameHandler(e:Event):void {
    vx = -int(leftIsPressed)*speed + int(rightIsPressed)*speed;
    vy = -int(upIsPressed)*speed + int(downIsPressed)*speed;
    player.x += vx;
    player.y += vy;
}

[EDIT] Added a more detailed example.

Of course in this case, if both left and right keys are pressed, this would result in a 0 offset.

垂暮老矣 2024-11-09 15:11:06

您可以监听KeyboardEvent.KEY_UP来确定按键何时被释放,这样您就可以跟踪哪些按键被按住。

You can listen to KeyboardEvent.KEY_UP to determine when a key has been released, so you can track which keys are being held.

装迷糊 2024-11-09 15:11:06

仅当按键从“向上”状态变为“向下”状态时,才会触发 KeyboardEvent.KEY_DOWN 事件,因此在您将手指从左键上抬起后,您的角色就会停止,因为没有触发导致角色移动的新 KEY_DOWN 事件。

您也许可以通过添加一个 KeyboardEvent.KEY_UP 事件来完成您的任务,该事件检查事件触发时是否按住了任何其他箭头键,但您可能会需要重构当前代码以避免重复。

The KeyboardEvent.KEY_DOWN event is only fired when a key goes from being in the "up" state to being in the "down" state, so your character stops after you lift your finger from the LEFT key because no new KEY_DOWN event has been triggered that would cause the character to move.

You might be able to accomplish what you're after by adding a KeyboardEvent.KEY_UP event that checks to see if any of the other arrow keys are being held down when the event fires, but you'd probably need to refactor your current code to avoid repeating yourself.

水水月牙 2024-11-09 15:11:06

要理解这种现象,需要对键盘输入所基于的事件模型有基本的了解。它在 AS3 中的工作方式与在 AS2 中的工作方式不同。

当您按下某个键时,会触发一个事件来调用某个函数 - 但仅在按下该键时触发一次。事件处理程序不检查按键是否被按下。 (尝试将 5 添加到对象的 x/y 值,而不是 onKeyDown 函数中的 vx;您会发现角色仅在按下某个键时移动,而不是在按住该键时移动。)

同样,当您释放某个键时,触发事件来调用函数 - 但仅在主动释放按键时触发一次。当钥匙放在那里时,不会触发任何事件。

在此基础上...您能找到当前问题的根源吗?

function onKeyUp(event:KeyboardEvent):void
{
    if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
    {
        vx = 0;
    }
    else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP)
    {
        vy = 0;
    }
}

如果你没找到,那就是这个街区。

    if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
    {
        vx = 0;
    }

该语句告诉您的程序,如果按下左右键,则将速度设置为零。但是,由于 Flash 仅在您第一次按下按键时检查按键,因此按住某个键不会触发 onKeyDown 函数。因此,如果你同时按住左键和右键,然后放开其中一个,你就会将速度设置为零。

Corey 在这里提供了逻辑解决方案,还有一些其他原因导致您想要使用切换布尔值来检查键盘输入(主要原因是您不会得到任何重复延迟)。

我希望这个答案能让你明白一点——一点知识可以带来伟大的事情。祝你好运。

Understanding this phenomenon requires a basic understanding of the Event model, which keyboard input is based upon. It works differently in AS3 than it does in AS2.

When you press a key, an event is fired to call a function - but it is only fired once, when the key is pressed. The event handlers don't check whether the key is held down. (Try adding 5 to your object's x/y value instead of your vx in your onKeyDown function; you'll find that your character only moves when you press a key, not while you hold it.)

Similarly, when you released a key, an event is fired to call a function - but it is only fired once, when the key is actively released. No events are fired when the key is just sitting there.

Building upon this...can you find the source of your current problem?

function onKeyUp(event:KeyboardEvent):void
{
    if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
    {
        vx = 0;
    }
    else if (event.keyCode == Keyboard.DOWN || event.keyCode == Keyboard.UP)
    {
        vy = 0;
    }
}

If you didn't find it, it's this block.

    if (event.keyCode == Keyboard.LEFT || event.keyCode == Keyboard.RIGHT)
    {
        vx = 0;
    }

This statement tells your program to set velocity equal to zero if either the right or the left keys are pressed. However, since Flash only checks for a keypress when you first press the key, holding down a key will not trigger your onKeyDown function. Hence, if you hold both left and right, and let one go, you're setting your velocity to zero.

Corey has the logical solution here, and there are some other reasons that you'll want to use a toggled boolean value to check keyboard input (the main reason is that you won't get any repeat delay).

I hope this answer cleared it up for you a little - a little knowledge can lead to great things. Good luck.

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