Avg. Rating 5.0

Problem

I'm looking for an example of using an accelerometer on a mobile device.

Solution

The good news if you want to understand the accelerometer is that the documentation on Adobe's site already provides some great examples. In fact, the initial portions of code for this tutorial are lifted straight from documentation which provides details on how to determine if the accelerometer is supported, establish the update interval and handler and even offers code that shows how to smooth out anomalies in the data it sends. Here's how to move an object around the screen using accelerometer data.

Detailed explanation

The below code utilizes all the example code from the  documentation to not only post the rolling X, Y and Z values from the accelerometer to a text field to read on screen, but also moves around an object I have on screen (an image of a golf ball) based upon the X and Y accelerometer inputs (I didn't require the Z axis data yet for the type of motion I wanted - for a good short explanation of the different axis, check  this forum reply).

if (Accelerometer.isSupported)
{
   accl = new Accelerometer();
   accl.setRequestedUpdateInterval(INTERVAL);
   accl.addEventListener(AccelerometerEvent.UPDATE,handleAccelUpdate);
}

function handleAccelUpdate(event:AccelerometerEvent) {
   accelRollingAvg(event);
   var newPosX:Number = golfBall.x - (rollingX*20);
   var newPosY:Number = golfBall.y + (rollingY*20);
   // set the new position for the object on the stage
   if ((newPosX > 0) && (newPosX < stage.stageWidth)) {
      golfBall.x = newPosX;
   }
   if ((newPosY > 0) && (newPosY < stage.stageHeight)) {
      golfBall.y = newPosY;
   }
}

function accelRollingAvg(event:AccelerometerEvent):void
{
   rollingX = (event.accelerationX * FACTOR) + (rollingX * ( 1 - FACTOR));
   rollingY = (event.accelerationY * FACTOR) + (rollingY * ( 1 - FACTOR));
   rollingZ = (event.accelerationZ * FACTOR) + (rollingZ * ( 1 - FACTOR));
} 

You'll notice above that I first set the new x and y positions of my golf ball into variables before I actually set the properties on the object. I do this because I want to prevent my golf ball from being able to roll offscreen - thus the if statements around setting the x and y values for golfBall. This works well but isn't perfect yet as it doesn't account for the width of the golf ball and therefore will keep the ball a few pixels off the edge of the screen on the left while letting it drift halfway off-screen to the right. Feel free to add logic to improve your code.

If you want a more detailed example, please check my post on my blog (where the above documentation was taken from) at  http://www.remotesynthesis.com/post.cfm/using-the-accelerometer-in-air-for-android-sample-application


+
This work is licensed under a Creative Commons Attribution-Noncommercial-Share Alike 3.0 Unported License. Permissions beyond the scope of this license, pertaining to the examples of code included within this work are available at Adobe.

Report abuse

Related recipes