flash.sensors

# Accelerometer - AS3

Package | flash.sensors
Class | public class Accelerometer
Inheritance | Accelerometer EventDispatcher 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.

AIR profile support: This feature is supported only on mobile devices. It is not supported on desktop or AIR for TV devices. See AIR Profile Support for more information regarding API support across multiple profiles.

View the examples

Learn more

Michael Chaize: AIR and the Accelerometer
Michael Chaize: Become an AIR Pilot

Related API Elements

isSupported
flash.events.AccelerometerEvent

Public Properties
Hide Inherited Public Properties
Show Inherited Public Properties

| Property | Defined By
--- | --- | ---
|  | constructor : 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

Public Methods
Hide Inherited Public Methods
Show Inherited Public Methods

| Method | Defined By
--- | --- | ---
|  | Accelerometer() Creates a new Accelerometer instance. | Accelerometer
|  | 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
|  | dispatchEvent(event:Event):Boolean Dispatches an event into the event flow. | EventDispatcher
|  | hasEventListener(type:String):Boolean Checks whether the EventDispatcher object has any listeners registered for a specific type of event. | EventDispatcher
|  | hasOwnProperty(name:String):Boolean Indicates whether an object has a specified property defined. | Object
|  | isPrototypeOf(theClass:Object):Boolean Indicates whether an instance of the Object class is in the prototype chain of the object specified as the parameter. | Object
|  | propertyIsEnumerable(name:String):Boolean Indicates whether the specified property exists and is enumerable. | Object
|  | removeEventListener(type:String, listener:Function, useCapture:Boolean = false):void Removes a listener from the EventDispatcher object. | EventDispatcher
|  | setPropertyIsEnumerable(name:String, isEnum:Boolean = true):void Sets the availability of a dynamic property for loop operations. | Object
|  | setRequestedUpdateInterval(interval:Number):void The setRequestedUpdateInterval method is used to set the desired time interval for updates. | Accelerometer
|  | toLocaleString():String Returns the string representation of this object, formatted according to locale-specific conventions. | Object
|  | toString():String Returns the string representation of the specified object. | Object
|  | valueOf():Object Returns the primitive value of the specified object. | Object
|  | willTrigger(type:String):Boolean Checks whether an event listener is registered with this EventDispatcher object or any of its ancestors for the specified event type. | EventDispatcher

Events
Click for more information on events
Hide Inherited Events
Show Inherited Events

| Event | Summary | Defined By
--- | --- | --- | ---
|  | activate | [broadcast event] Dispatched when the Flash Player or AIR application gains operating system focus and becomes active. | EventDispatcher
|  | deactivate | [broadcast event] Dispatched when the Flash Player or AIR application operating loses system focus and is becoming inactive. | EventDispatcher
|  | status | Dispatched when an accelerometer changes its status. | Accelerometer
|  | update | 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`

More examples

Checking accelerometer support
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.
Copy

    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`

Related API Elements

status
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` is set to 0, then the minimum supported update interval is used.

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:

Property | Value
--- | ---
`bubbles` | `false`
`cancelable` | `false`; there is no default behavior to cancel.
`code` | A description of the object's status.
`currentTarget` | The object that is actively processing the Event object with an event listener.
`level` | The category of the message, such as `"status"`, `"warning"` or `"error"`.
`target` | The 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:

Property | Value
--- | ---
`bubbles` | `false`
`cancelable` | `false`; there is no default behavior to cancel.
`currentTarget` | The object that is actively processing the Event object with an event listener.
`timestamp` | The timestamp of the Accelerometer update.
`accelerationX` | The acceleration value in Gs (9.8m/sec/sec) along the x-axis.
`accelerationY` | The acceleration value in Gs (9.8m/sec/sec) along the y-axis.
`accelerationZ` | The acceleration value in Gs (9.8m/sec/sec) along the z-axis.

More examples

Detecting accelerometer changes
Examples How to use this example
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.
Copy

    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;
            }
        }
    }

© 2015 Adobe Systems Incorporated. All rights reserved.
Mon Apr 10 2017, 12:28 PM -07:00