What is the most efficient way to test combined arrow directions in ActionScript 3.0?

I need to track the direction that the user points with the four directional arrow keys on the keyboard in ActionScript 3.0, and I want to know the most efficient and effective way to do this.

I have a few ideas on how to do this, and I'm not sure what would be better. I found that when tracking Keyboard.KEY_DOWN events, the event is repeated until the key is working, so the event function is also repeated. This violated the method that I originally chose to use, and the methods I could think of require a large number of comparison operators.

The best way I could think of is to use bitwise operators in the uint variable. That's what I think

var _direction:uint = 0x0; // The Current Direction

This is the current direction variable. In the Keyboard.KEY_DOWN event handler, I’ll ask you to check which key is turned off and use the bitwise AND operation to see if it is already on, and if not, I will add it using a basic add. Thus, up will be 0x1, and down will be 0x2, and up and down will be 0x3, for example. It will look something like this:

private function keyDownHandler(e:KeyboardEvent):void
{
    switch(e.keyCode)
    {
        case Keyboard.UP:
            if(!(_direction & 0x1)) _direction += 0x1;
            break;
        case Keyboard.DOWN:
            if(!(_direction & 0x2)) _direction += 0x2;
            break;
        // And So On...
    }
}

KeyUpHandler if, , , . , switch, 0 15 . , , if keyDown .

private function checkDirection():void
{
    switch(_direction)
    {
        case 0:
            // Center
            break;
        case 1:
            // Up
            break;
        case 2:
            // Down
            break;
        case 3:
            // Up and Down
            break;
        case 4:
            // Left
            break;
        // And So On...
    }
}

?

0
1

, , KEY_DOWN KEY_UP . , ( ).

, , ; ( ). , , :

function enterFrameCallback(e:Event):void
{
    var speed:Number = 1.0;     // net pixels per frame movement

    thing.x += (
        -(int)Input.isKeyDown(Keyboard.LEFT)
        +(int)Input.isKeyDown(Keyboard.RIGHT)
    ) * speed;
    thing.y += (
        -(int)Input.isKeyDown(Keyboard.UP)
        +(int)Input.isKeyDown(Keyboard.DOWN)
    ) * speed;
}

. , (, , X , X ), :

function enterFrameCallback(e:Event):void
{
    var speed:Number = 1.0;     // net pixels per frame movement
    var displacement:Point = new Point();

    displacement.x = (
        -(int)Input.isKeyDown(Keyboard.LEFT)
        +(int)Input.isKeyDown(Keyboard.RIGHT)
    );
    displacement.y = (
        -(int)Input.isKeyDown(Keyboard.UP)
        +(int)Input.isKeyDown(Keyboard.DOWN)
    );

    displacement.normalize(speed);

    thing.x += displacement.x;
    thing.y += displacement.y;
}

, ( init ). , ; , :

/*******************************************************************************
* DESCRIPTION: Defines a simple input class that allows the programmer to
*              determine at any instant whether a specific key is down or not,
*              or if the mouse button is down or not (and where the cursor
*              is respective to a certain DisplayObject).
* USAGE: Call init once before using any other methods, and pass a reference to
*        the stage. Use the public methods commented below to query input states.
*******************************************************************************/


package
{
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.display.Stage;
    import flash.geom.Point;
    import flash.display.DisplayObject;


    public class Input
    {
        private static var keyState:Array = new Array();
        private static var _mouseDown:Boolean = false;
        private static var mouseLoc:Point = new Point();
        private static var mouseDownLoc:Point = new Point();


        // Call before any other functions in this class:
        public static function init(stage:Stage):void
        {
            stage.addEventListener(KeyboardEvent.KEY_DOWN, onKeyDown, false, 10);
            stage.addEventListener(KeyboardEvent.KEY_UP, onKeyUp, false, 10);
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown, false, 10);
            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp, false, 10);
            stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove, false, 10);
        }

        // Call to query whether a certain keyboard key is down.
        //     For a non-printable key: Input.isKeyDown(Keyboard.KEY)
        //     For a letter (case insensitive): Input.isKeyDown('A')
        public static function isKeyDown(key:*):Boolean
        {
            if (typeof key == "string") {
                key = key.toUpperCase().charCodeAt(0);
            }
            return keyState[key];
        }

        // Property that is true if the mouse is down, false otherwise:
        public static function get mouseDown():Boolean
        {
            return _mouseDown;
        }

        // Gets the current coordinates of the mouse with respect to a certain DisplayObject.
        // Leaving out the DisplayObject paramter will return the mouse location with respect
        // to the stage (global coordinates):
        public static function getMouseLoc(respectiveTo:DisplayObject = null):Point
        {
            if (respectiveTo == null) {
                return mouseLoc.clone();
            }
            return respectiveTo.globalToLocal(mouseLoc);
        }

        // Gets the coordinates where the mouse was when it was last down or up, with respect
        // to a certain DisplayObject. Leaving out the DisplayObject paramter will return the
        // location with respect to the stage (global coordinates):
        public static function getMouseDownLoc(respectiveTo:DisplayObject = null):Point
        {
            if (respectiveTo == null) {
                return mouseDownLoc.clone();
            }
            return respectiveTo.globalToLocal(mouseDownLoc);
        }

        // Resets the state of the keyboard and mouse:
        public static function reset():void
        {
            for (var i:String in keyState) {
                keyState[i] = false;
            }
            _mouseDown = false;
            mouseLoc = new Point();
            mouseDownLoc = new Point();
        }


        /////  PRIVATE METHODS BEWLOW  /////

        private static function onMouseDown(e:MouseEvent):void
        {
            _mouseDown = true;
            mouseDownLoc = new Point(e.stageX, e.stageY);
        }

        private static function onMouseUp(e:MouseEvent):void
        {
            _mouseDown = false;
            mouseDownLoc = new Point(e.stageX, e.stageY);
        }

        private static function onMouseMove(e:MouseEvent):void
        {
            mouseLoc = new Point(e.stageX, e.stageY);
        }

        private static function onKeyDown(e:KeyboardEvent):void
        {
            keyState[e.keyCode] = true;
        }

        private static function onKeyUp(e:KeyboardEvent):void
        {
            keyState[e.keyCode] = false;
        }
    }
}
+1

Source: https://habr.com/ru/post/1740581/


All Articles