The code below moves a movieclip based on the accelerometer. How to detect in which direction it moves or if it stands still?
import flash.sensors.Accelerometer;
import flash.events.AccelerometerEvent;
var my_acc:Accelerometer = new Accelerometer();
my_acc.setRequestedUpdateInterval(50);
my_acc.addEventListener(AccelerometerEvent.UPDATE, onAccUpdate);
function onAccUpdate(e:AccelerometerEvent):void{
my_dot.x -= (e.accelerationX*10);
if (my_dot.x < 0) {
my_dot.x = 0;
} else if (my_dot.x > stage.stageWidth) {
my_dot.x = stage.stageWidth;
}
}
You get the real acceleration vector with
Then, you get the direction in degrees with
Try the following:
and note that you get
directionin radians!If you want deegrees:
var directionDeg = direction * 180/Math.PIIf your
accelerationYis negative, you need to change the sign ofdirectionDegand (if you want) add 360 to always get a positive number.