Simple Accelerometer Example

I’m adding some custom accelerometer/orientation behavior to the iReverse game I’m working on, so I threw together a simple test app to output accelerometer data. I figured I’d post the code in case anyone else was working on something similar.

Code and screenshot below:

    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.sensors.Accelerometer;
    import flash.text.TextField;
    import flash.text.TextFormat;

    public class AccelerometerTest extends Sprite
        private var textFormat:TextFormat;
        private var output:TextField;
        private var accelerometer:Accelerometer;

            public function AccelerometerTest()
                this.stage.scaleMode = StageScaleMode.NO_SCALE;
                this.stage.align = StageAlign.TOP_LEFT;

                this.textFormat = new TextFormat();
                this.textFormat.color = 0x000000;
                this.textFormat.font = "Helvetica";
                this.textFormat.size = 20;

                this.output = new TextField();
                this.output.defaultTextFormat = this.textFormat;

                this.stage.addEventListener(Event.RESIZE, doLayout);

                this.accelerometer = new Accelerometer();
                accelerometer.addEventListener(AccelerometerEvent.UPDATE, onAccelerometerUpdate);

            private function doLayout(e:Event):void
                this.output.width = this.stage.stageWidth;
                this.output.height = this.stage.stageHeight;
                this.output.x = 0;
                this.output.y = 0;

            private function onAccelerometerUpdate(e:AccelerometerEvent):void
                var str:String = "x: " + e.accelerationX;
                str += "\ny: " + e.accelerationY;
                str += "\nz: " + e.accelerationZ;
                this.output.text = str;

2 Responses to Simple Accelerometer Example

  1. I tried your example, and also the similar example on, without success.My platform is Windows 7, and it has an accelerometer with a Windows 7 Sensor Framework compliant accelerometer device driver.I always get Accelerometer.isSupported returning false, and this.accelerometer.muted returning true. Even though the accelerometer does appear in Control Panel > Location and Sensors and the privacy check-mark is set to On/Enabled.Any ideas? Is accelerometer supported in Flash 10.1 RC2 on Win 7, or only on mobile phones??

  2. zproxy says:

    Hey Jim, with android 2.2 I cannot make this work within flash in the webbrowser. Any ideas why?