Updates and Screen Rendering, Setting Boundaries

The device defines the frequency of sensor updates. You cannot obtain this information through the Android or AIR API. You can, however, overwrite it to conserve battery life. The value is in milliseconds:


Do not use every AccelerometerEvent update to redraw the screen. This drains the battery and moves too fast for the frame rate at which the screen is redrawn. Instead, store the value on update and redraw the screen on the ENTER_FRAME event. You will notice a much smoother animation and better overall performance:

import flash.events.Event;
import flash.events.AccelerometerEvent;
import flash.sensors.Accelerometer;
const MULTIPLIER:Number = 20.0;
var vx:Number = 0.0;
var vy:Number = 0.0;
var accelerometer:Accelerometer;
accelerometer = new Accelerometer();
accelerometer.addEventListener(AccelerometerEvent.UPDATE, onUpdate);
stage.addEventListener(Event.ENTER_FRAME, onEnterFrame);
function onUpdate(event:AccelerometerEvent):void {
vx = event.accelerationX * MULTIPLIER;
vy = event.accelerationY * MULTIPLIER;
function onEnterFrame(event:Event):void {
ball.x -= vx;
ball.y += vy;

Notice the event.stopPropagation method called in the onUpdate function. It is important to eliminate unnecessary steps, especially if you have a deep displayList.

Setting Boundaries

Keep your animation within the boundaries of your screen. Set the values of the boundaries, and only move the ball if the position change is within them:

var radius:int;
var xBounds:int;
var yBounds:int;
var newX:Number = 0.0;
var newY:Number = 0.0;
radius = ball.width;
xBounds = stage.stageWidth – radius;
yBounds = stage.stageHeight – radius;
function onEnterFrame(event:Event):void {
newX = ball.x – vx;
newY = ball.y + vy;
if (newX > radius && newX < xBounds) {
ball.x = newX;
if (newY > radius && newY < yBounds) {
ball.y = newY;

In this example, the boundaries are defined by xBounds and yBounds. They represent the dimension of the stage minus the radius of the ball being animated.