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 |
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.
Learn more
Related API Elements
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 | ||
prototype : Object [static]
A reference to the prototype object of a class or function object. | Object |
Method | Defined By | ||
---|---|---|---|
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 | ||
Dispatches an event into the event flow. | EventDispatcher | ||
Checks whether the EventDispatcher object has any listeners registered for a specific type
of event. | EventDispatcher | ||
Indicates whether an object has a specified property defined. | Object | ||
Indicates whether an instance of the Object class is in the prototype chain of the object specified
as the parameter. | Object | ||
Indicates whether the specified property exists and is enumerable. | Object | ||
Removes a listener from the EventDispatcher object. | EventDispatcher | ||
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 | ||
Returns the string representation of this object, formatted according to locale-specific conventions. | Object | ||
Returns the string representation of the specified object. | Object | ||
Returns the primitive value of the specified object. | Object | ||
Checks whether an event listener is registered with this EventDispatcher object or any of
its ancestors for the specified event type. | EventDispatcher |
Event | Summary | Defined By | ||
---|---|---|---|---|
[broadcast event] Dispatched when the Flash Player or AIR application gains operating system focus and becomes active. | EventDispatcher | |||
[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 |
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
Example ( How to use this example )
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
Related API Elements
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.
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.
|
status | Event |
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.
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 |
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).
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
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; } } }
Mon Nov 28 2011, 06:48 AM -08:00