Tutorials, extensions, and source files for ActionScript, Flash, and other Adobe products.

 

senocular.com ActionScript Library

direction.as

Name: direction() - returns angle of arrow keys in degrees
Author: senocular: www.senocular.com
Date: 1899-12-31T00:08:18.000
Documentation:
Key DIRECTION: returns angle of direction from arrow key input.
returns from -135 to 180 in 45 degree increments.
 
Arguments:
- def: (optional) if given, returns the default angle if no keys are pressed
 
Update:
- changed argument from "default" to "def" for MX compatability
- also fixed LEFT and RIGHT to coincide with direction movement

Example:
onClipEvent(enterFrame){
	_rotation = Key.direction();
}

1
2
3
4
5
6
7
Key.direction = function(def){
	var DU = Key.isDown(Key.DOWN) - Key.isDown(Key.UP); // -1, 0 or 1 for DU based on up & down arrows
	var RL = Key.isDown(Key.RIGHT) - Key.isDown(Key.LEFT); // -1, 0 or 1 for LR based on left & right arrows
	if (DU) return 90*DU-45*LR*DU; // if up or down is pressed
	else if (LR) return 90-90*LR; // if just left or right and no up or down
	else return def; // if nothing is pressed
}