Using Accelerometer

The following code sample demonstrates the functioning of the accelerometer. The code first checks if the device supports accelerometer. If it does, then an event listener is registered to listen for changes in the acceleration after the application has started. Whenever the listener detects a change, the accListener function is called and it returns the different acceleration information. The time interval for acceleration updates is set at 5 seconds. For more information, see Accelerometer.

import flash.sensors.*; 
if ((Accelerometer.isSupported) 
{ 
    var acc:Accelerometer =  new Accelerometer(); 
    function accListener(evt:AccelerometerEvent) 
    { 
        trace("AccelerationX: " + evt.accelerationX.toString()); 
        trace("AccelerationY: " + evt.accelerationY.toString()); 
        trace("AccelerationZ: " + evt.accelerationZ.toString()); 
        trace("Timestamp: "+evt.timestamp.toString()); 
    } 
    acc.addEventListener(AccelerometerEvent.UPDATE, accListener); 
    acc.setRequestedUpdateInterval(5000); 
    acc.removeEventListener(AccelerometerEvent.UPDATE, accListener); 
} 
else 
{ 
    trace("Accelerometer not supported."); 
}

For more information about the Accelerometer and AccelerometerEvent classes, see Adobe ActionScript 3.0 Reference for the Adobe Flash Platform.