Adobe® Flex® 4 Language Reference
Show Packages and Classes List |  Packages  |  Classes  |  Index  |  Appendixes
flash.sensors 
Accelerometer 
Packageflash.sensors
Classpublic class Accelerometer
InheritanceAccelerometer Inheritance EventDispatcher Inheritance Object

Language Version: ActionScript 3.0
Runtime Versions: AIR 2, Flash Player 10.1, Flash Lite 4

The Accelerometer class dispatches events based on activity detected by the device's motion sensor. This data represents the device's location or movement along a 3-dimensional axis. When the device moves, the sensor detects this movement and returns acceleration data. The Accelerometer class provides methods to query whether or not accelerometer is supported, and also to set the rate at which acceleration events are dispatched.

Note: Use the Accelerometer.isSupported property to test the runtime environment for the ability to use this feature. While the Accelerometer class and its members are accessible to the Runtime Versions listed for each API entry, the current environment for the runtime determines the availability of this feature. For example, you can compile code using the Accelerometer class properties for Flash Player 10.1, but you need to use the Accelerometer.isSupported property to test for the availability of the Accelerometer feature in the current deployment environment for the Flash Player runtime. If Accelerometer.isSupported is true at runtime, then Accelerometer support currently exists.

View the examples

See also



Public Properties
 PropertyDefined By
 Inheritedconstructor : Object
A reference to the class object or constructor function for a given object instance.
Object
  isSupported : Boolean
[static] [read-only] The isSupported property is set to true if the accelerometer sensor is available on the device, otherwise it is set to false.
Accelerometer
  muted : Boolean
[read-only] Specifies whether the user has denied access to the accelerometer (true) or allowed access (false).
Accelerometer
 Inheritedprototype : Object
[static] A reference to the prototype object of a class or function object.
Object
Public Methods
 MethodDefined By
  
Creates a new Accelerometer instance.
Accelerometer
 Inherited
addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void
Registers an event listener object with an EventDispatcher object so that the listener receives notification of an event.
EventDispatcher
 Inherited
Dispatches an event into the event flow.
EventDispatcher
 Inherited
Checks whether the EventDispatcher object has any listeners registered for a specific type of event.
EventDispatcher
 Inherited
Indicates whether an object has a specified property defined.
Object
 Inherited
Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter.
Object
 Inherited
Indicates whether the specified property exists and is enumerable.
Object
 Inherited
removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void
Removes a listener from the EventDispatcher object.
EventDispatcher
 Inherited
Sets the availability of a dynamic property for loop operations.
Object
  
The setRequestedUpdateInterval method is used to set the desired time interval for updates.
Accelerometer
 Inherited
Returns the string representation of this object, formatted according to locale-specific conventions.
Object
 Inherited
Returns the string representation of the specified object.
Object
 Inherited
Returns the primitive value of the specified object.
Object
 Inherited
Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type.
EventDispatcher
Events
 Event Summary Defined By
 Inherited[broadcast event] Dispatched when the Flash Player or AIR application gains operating system focus and becomes active.EventDispatcher
 Inherited[broadcast event] Dispatched when the Flash Player or AIR application operating loses system focus and is becoming inactive.EventDispatcher
  Dispatched when an accelerometer changes its status.Accelerometer
  The update event is dispatched in response to updates from the accelerometer sensor.Accelerometer
Property Detail

isSupported

property
isSupported:Boolean  [read-only]

Language Version: ActionScript 3.0
Runtime Versions: AIR 2, Flash Player 10.1, Flash Lite 4

The isSupported property is set to true if the accelerometer sensor is available on the device, otherwise it is set to false.



Implementation
    public static function get isSupported():Boolean

Example  ( How to use this example )
The following example uses the Accelerometer.isSupported property to test for Accelerometer support at runtime. If the current environment supports the Accelerometer feature, then an event listener is added to the Accelerometer object, and the associated handler populates the text field with the timestamp and acceleration values. Otherwise, the text field indicates that the feature is not supported in the current environment.
var myTextField:TextField = new TextField();
myTextField.width = 200;
addChild(myTextField);
var acc1:Accelerometer = new Accelerometer();
var isSupported:Boolean = Accelerometer.isSupported;
checksupport();

function checksupport():void {
    if (isSupported) {
        myTextField.text = "Accelerometer feature supported";
        acc1.addEventListener(AccelerometerEvent.UPDATE, updateHandler);
    } else {
        myTextField.text = "Accelerometer feature not supported";
    }
}

function updateHandler(evt:AccelerometerEvent):void {
    myTextField.text = String("at: " + evt.timestamp + "\n" + "acceleration X: " + evt.accelerationX + "\n" + "acceleration Y: " + evt.accelerationY + "\n" + "acceleration Z: " + evt.accelerationZ);

}

muted

property 
muted:Boolean  [read-only]

Language Version: ActionScript 3.0
Runtime Versions: AIR 2, Flash Player 10.1

Specifies whether the user has denied access to the accelerometer (true) or allowed access (false). When this value changes, a status event is dispatched.



Implementation
    public function get muted():Boolean

See also

Constructor Detail

Accelerometer

()Constructor
public function Accelerometer()

Language Version: ActionScript 3.0
Runtime Versions: AIR 2, Flash Player 10.1, Flash Lite 4

Creates a new Accelerometer instance.

Method Detail

setRequestedUpdateInterval

()method
public function setRequestedUpdateInterval(interval:Number):void

Language Version: ActionScript 3.0
Runtime Versions: AIR 2, Flash Player 10.1, Flash Lite 4

The setRequestedUpdateInterval method is used to set the desired time interval for updates. The time interval is measured in milliseconds. The update interval is only used as a hint to conserve the battery power. The actual time between acceleration updates may be greater or lesser than this value. Any change in the update interval affects all registered listeners. You can use the Accelerometer class without calling the setRequestedUpdateInterval() method. In this case, the application receives updates based on the device's default interval.

Parameters

interval:Number — The requested update interval. If interval <= 0, calling this method has no effect.


Throws
ArgumentError — The specified interval is less than zero.
Event Detail

status

Event
Event Object Type: flash.events.StatusEvent
property StatusEvent.type = flash.events.StatusEvent.STATUS

Language Version: ActionScript 3.0
Runtime Versions: AIR 2, Flash Player 10.1, Flash Lite 4

Dispatched when an accelerometer changes its status.

Note: On some devices, the accelerometer is always available. On such devices, an Accelerometer object never dispatches a status event.

Defines the value of the type property of a status event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
codeA description of the object's status.
currentTargetThe object that is actively processing the Event object with an event listener.
levelThe category of the message, such as "status", "warning" or "error".
targetThe object reporting its status.

update

Event  
Event Object Type: flash.events.AccelerometerEvent
property AccelerometerEvent.type = flash.events.AccelerometerEvent.UPDATE

Language Version: ActionScript 3.0
Runtime Versions: AIR 2, Flash Player 10.1, Flash Lite 4

The update event is dispatched in response to updates from the accelerometer sensor. The event is dispatched in the following circumstances:

  • When a new listener function is attached through addEventListener(), this event is delivered once to all the registered listeners for providing the current value of the accelerometer.
  • Whenever accelerometer updates are obtained from the platform at device determined intervals.
  • Whenever the application misses a change in the accelerometer (for example, the runtime is resuming after being idle).

Defines the value of the type property of a AccelerometerEvent event object.

This event has the following properties:

PropertyValue
bubblesfalse
cancelablefalse; there is no default behavior to cancel.
currentTargetThe object that is actively processing the Event object with an event listener.
timestampThe timestamp of the Accelerometer update.
accelerationXThe acceleration value in Gs (9.8m/sec/sec) along the x-axis.
accelerationYThe acceleration value in Gs (9.8m/sec/sec) along the y-axis.
accelerationZThe acceleration value in Gs (9.8m/sec/sec) along the z-axis.
AccelerometerExample.as

In the following example, the application moves a Sprite based on accelerometer update events. The accelerometer update events indicate movement of the device.
package 
{
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.AccelerometerEvent;
    import flash.sensors.Accelerometer;

    public class AccelerometerTest extends Sprite
    {
        private var ball:Sprite;
        private var accelerometer:Accelerometer;        
        private var xSpeed:Number = 0;
        private var ySpeed:Number = 0;
        private const RADIUS = 20;
        
        public final function AccelerometerTest()
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
            createBall();
                                    
            if (Accelerometer.isSupported)
            {
                accelerometer = new Accelerometer();
                accelerometer.addEventListener(AccelerometerEvent.UPDATE, accUpdateHandler);
                stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
            }
        }
        
        private final function createBall():void
        {
             ball = new Sprite();
             ball.graphics.beginFill(0xFF0000);
             ball.graphics.drawCircle(0, 0, RADIUS);
             ball.cacheAsBitmap = true;
             ball.x = stage.stageWidth / 2;
             ball.y = stage.stageHeight / 2;
             addChild(ball);
        }

        private final function enterFrameHandler(event:Event):void
        {
            event.stopPropagation();
            moveBall();
        }
        private final function moveBall():void
        {
            var newX:Number = ball.x + xSpeed;
            var newY:Number = ball.y + ySpeed;
            if (newX < 20)
            {
                ball.x = RADIUS;
                xSpeed = 0;
            }
            else if (newX > stage.stageWidth - RADIUS)
            {
                ball.x = stage.stageWidth - RADIUS;
                xSpeed = 0;
            }
            else
            {
                ball.x += xSpeed;
            }
            
            if (newY < RADIUS)
            {
                ball.y = RADIUS;
                ySpeed = 0;
            }
            else if (newY > stage.stageHeight - RADIUS)
            {
                ball.y = stage.stageHeight - RADIUS;
                ySpeed = 0;
            }
            else
            {
                ball.y += ySpeed;
            }
        }

        private final function accUpdateHandler(event:AccelerometerEvent):void
        {
            xSpeed += event.accelerationX * 2;
            ySpeed -= event.accelerationY * 2;
        }
    }
}