AS3键盘控制键盘

2023-09-08 14:37:59 作者:独爱、单身

我有一个AS3的功能控制的影片剪辑的键盘:

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

包 {

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

}

该工程确定,但主要的问题是,当你preSS右键(并按住),那么preSS左键,人物会向左移动,但是当你松开左键(用正确的关键还是按住)的字符只是停止。我怎样才能让这样的人物再次开始移动到右侧,在这种情况下(如果进出口仍按住右键后,香港专业教育学院发布的左键)

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)

感谢

推荐答案

您可以把你的'玩家'定位code到 Event.ENTER_FRAME 循环和使用 KeyboardEvent.KEY_DOWN KeyboardEvent.KEY_UP 来设置布尔值,并在诸如环位置的球员:

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

增加了更多的细节。

Added a more detailed example.

当然,在这种情况下,如果两个左右按键都pssed $ P $,这将导致 0 偏移。

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