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;
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;
}
}
KeyUpHandler if, , , . , switch, 0 15 . , , if keyDown .
private function checkDirection():void
{
switch(_direction)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
}
}
?