Updates and Screen Rendering, Setting Boundaries

Must Read


One of the navigation methods unique to mobile devices is the ability to interact with an application via gestures...

US Government Cuts Out Middleman With Student Loan Debt Consolidation

The ability to square up a student loan, especially a defaulted upon student loan, is one of the largest...

Overcoming Bad Credit Scrores with a Home Equity Loan or Second Mortgage

If you have bad credit, but want to save some money and repair your credit score, take out a...

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.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.



Please enter your comment!
Please enter your name here

Latest News

Top 10 Provinces of Thailand Not to be missed for Travel

10 SuratthaniThe source of the proverbial Chaiya salted egg. Surat Thani is a province with many attractions,...

Underlight As Accent, For Power and The Main Light for Photography

Underlighting, in which fill or accent light comes from under the topic, is not widely used technique in the traditional Portraits, even though it will have...

How To Fix Overexposure As A Creative Tool, The Complete Guide

As an creative tool, overexposure is sort of underrated. What I’m close to propose could be a deliberate and well thought out technique for...

7D Mark II is Canon’s best DSLR cameras without full-frame sensor.

The expected long-awaited Canon EOS 7D Mark II are shipping in November for $ 1,799 without a lens. With a higher price tag, you...

Low key photography and How to isolate your subject.

Low key photography doesn’t depend on underexposure to make its point; the key to low key is that the majority of tones, even correctly...

More Articles Like This