What is the most effective way to test for combined keyboard arrow direction in ActionScript 3.0?
I need to monitor the direction a user is indicating using the four directional arrow keys on a keyboard in ActionScript 3.0 and I want to know the most efficient and effective way to do this.

I've got several ideas of how to do it, and I'm not sure which would be best. I've found that when tracking Keyboard.KEY_DOWN events, the event repeats as long as the key is down, so the event function is repeated as well. This broke the method I had originally chosen to use, and the methods I've been able to think of require a lot of comparison operators.

The best way I've been able to think of would be to use bitwise operators on a uint variable. Here's what I'm thinking

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

That's the current direction variable. In the Keyboard.KEY_DOWN event handler I'll have it check what key is down, and use a bitwise AND operation to see if it's already toggled on, and if it's not, I'll add it in using basic addition. So, up would be 0x0001 and down would be 0x0010 and both up and down would be 0x0011, for example. It would look something like this:

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

The keyUpHandler wouldn't need the if operation since it only triggers once when the key goes up, instead of repeating. I'll be able to test the current direction by using a switch statement labeled with numbers from 0 to 8 for the nine possible directions. That should work, but it doesn't seem terribly elegant to me, given all of the if statements in the repeating keyDown event handler.

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

Is there a better way to do this?
What is the most effective way to test for combined keyboard arrow direction in ActionScript 3.0?
===

I need to monitor the direction a user is indicating using the four directional arrow keys on a keyboard in ActionScript 3.0 and I want to know the most efficient and effective way to do this.

I've got several ideas of how to do it, and I'm not sure which would be best. I've found that when tracking Keyboard.KEY_DOWN events, the event repeats as long as the key is down, so the event function is repeated as well. This broke the method I had originally chosen to use, and the methods I've been able to think of require a lot of comparison operators.

The best way I've been able to think of would be to use bitwise operators on a uint variable. Here's what I'm thinking

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

That's the current direction variable. In the Keyboard.KEY_DOWN event handler I'll have it check what key is down, and use a bitwise AND operation to see if it's already toggled on, and if it's not, I'll add it in using basic addition. So, up would be 0x0001 and down would be 0x0010 and both up and down would be 0x0011, for example. It would look something like this:

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

The keyUpHandler wouldn't need the if operation since it only triggers once when the key goes up, instead of repeating. I'll be able to test the current direction by using a switch statement labeled with numbers from 0 to 8 for the nine possible directions. That should work, but it doesn't seem terribly elegant to me, given all of the if statements in the repeating keyDown event handler.

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

Is there a better way to do this?