The MobileAccelerometer.as script will help you to know if a mobile device, with accelerometer, is turning left, right or up. It’s just a quick script that will save you time.
How to use MobileAccelerometer.as
In order to use it, you will need to have a static instance of mono in the Main.as script. Once you have that, create an instance of the script wherever you want. Then, turn on the script by calling the function turnOn. This function will recieve as parameters the tolerance in x and y before showing true, though this values are optional because it already has default ones. Once it is turned on, you will only need to check the public values called keyLeft, keyRight and keyUp to know where is it looking.
For example we can do the following:
package { import Mono.Controls.MobileAccelerometer; import Mono.Mono; import flash.display.Sprite; import flash.utils.Dictionary; public class Main extends Sprite { public static var mono:Mono; public var mobileAccelerometer:MobileAccelerometer; public function Main() { mono = new Mono(stage); mobileAccelerometer = new MobileAccelerometer(); mobileAccelerometer.turnOn(0.05, 0.05); } } }
Now we can check the values by just doing, for example, this:
if(mobileAccelerometer.keyUp) { //it is up, do something }
Public Functions
- turnOn(toleranceX:Number=0.10, toleranceY:Number=0.10):void
- turnOff():void
Public Vars
- keyLeft:Boolean
- keyRight:Boolean
- keyUp:Boolean
Private Functions
- checkSupport():Boolean
- updateHandler(e:AccelerometerEvent):void
Private Vars
- _isSupported:Boolean
- _acc1:Accelerometer
- _turnedOn:Boolean
- _xTolerance:Number
- _yTolerance:Number