The Accelerometer Class

Must Read

Just post!

The flash.sensors.Accelerometer class is an addition to ActionScript to support receiving messages from the device’s motion sensor. It is a subclass of the EventDis patcher class. The class is a new Event class that returns the sensor’s updated information.

Measuring acceleration along the perpendicular axes on an Android device

When using these classes, you should first check at runtime that the user’s device has a motion sensor and that it is available for reading. Look at the value of the isSuppor ted boolean property:

import flash.sensors.Accelerometer;
if (Accelerometer.isSupported == false) {

The accelerometer muted property is false if the user blocks access to the accelerometer sensor. Android devices do not provide an option to disable access at the time of this writing, so you can ignore it.

Now create an instance of the Accelerometer class. This is essential to make the accelerometer variable a class variable, not a local variable, to guarantee that it is not removed from memory:

var accelerometer:Accelerometer;
accelerometer = new Accelerometer();

Next, set an AccelerometerEvent to receive updates for the device sensor:

accelerometer.addEventListener(AccelerometerEvent.UPDATE, onUpdate);

A timer starts ticking the moment the application is initialized and returns a time stamp value in milliseconds. This is useful if you want to average activity over time:

function onUpdate(event:AccelerometerEvent):void {

The event returns a floating point for each axis that ranges between ‒1.0 and 1.0. The values indicate the force pulling your device from both gravity and the user’s motion. If your device is not moving, the only acceleration measured is gravity that is, of course, active.

Motion values are not locked into the default portrait aspect ratio. If the screen orientation changes, the Accelerometer class takes care of adjusting the values so that they are set according to the stage axis, not the device axis.

- Advertisement -

Latest News

Yadea Becomes Official Supplier for Hangzhou Asian Games

Yadea Named Official Supplier for Hangzhou 2023 Asian Games, Hosts Fun Run Events in Sri Lanka, Laos, and Vietnam Yadea,...
- Advertisement -

More Articles Like This

- Advertisement -