Using accelerometer with Flash Lite 4.0

We had the launch of the Nokia N8, officially the first device to support Flash Lite 4.0. Besides the Nokia N8, we also have a Nokia C7 and Nokia E7, all running the Symbian^3 and Adobe Flash Lite 4.0 is already installed. This implies the possibility of developing applications for Symbian^3 using ActionScript 3.0 running on the browser or standalone.
We must look to the fact that Flash Lite 4.0 is not the Flash Player 10.1, they have some significant differences:

  • Flash Lite supports some features partially.
  • Flash Lite adds some features to work specifically with mobile devices.

Flash Lite 4.0 is based on Flash Player 10, having features that were introduced in Flash Player 9 and 10. Among the resources available are:

  • Multi-touch support
  • Flash Player 10 text engine
  • Using inline text input
  • RTMP data channel
  • RTMPE
  • RTMPT and RTMPTE
  • Multi bit-rate streaming
  • Geolocation
  • Accelerometer
  • SharedObject Remote(yessss! 😀 )

Also see partially supported classes and the Unsupported classes.
Come to our first example using the new capabilities of Flash Lite 4.0, in which case the accelerometer.

  • Create a file of type Flash Lite 4.0 in Flash CS5 or in Adobe Device Central CS5.
  • Add three dynamic text fields in them will show the coordinates of x, y and z.

We begin our coding, we have a method to create a ball that will suffer the effects of the accelerometer

[ACTIONSCRIPT3]
function createBall():void
{
ball = new Sprite();
ball.graphics.beginFill(0xFF0000);
ball.graphics.drawCircle(0, 0, RADIUS);
ball.cacheAsBitmap = true;
ball.x = stage.stageWidth / 2;
ball.y = stage.stageHeight / 2;
addChild(ball);
}
[/ACTIONSCRIPT3]
Then check if the device has an accelerometer to add the listeners that handle the sensor data and update the positions of the ball.
[ACTIONSCRIPT3]
function AccelerometerTest()
{
stage.scaleMode = StageScaleMode.NO_SCALE;
stage.align = StageAlign.TOP_LEFT;

createBall();

if (Accelerometer.isSupported)
{
accelerometer = new Accelerometer();
accelerometer.addEventListener(AccelerometerEvent.UPDATE, accUpdateHandler);
stage.addEventListener(Event.ENTER_FRAME, enterFrameHandler);
}
}
[/ACTIONSCRIPT3]
And the rest of the code.
[ACTIONSCRIPT3]
function enterFrameHandler(event:Event):void
{
event.stopPropagation();
moveBall();
}
function moveBall():void
{
var newX:Number = ball.x + xSpeed;
var newY:Number = ball.y + ySpeed;
if (newX <20) { ball.x = RADIUS; xSpeed = 0; } else if (newX> stage.stageWidth – RADIUS)
{
ball.x = stage.stageWidth – RADIUS;
xSpeed = 0;
}
else
{
ball.x += xSpeed;
}

if (newY stage.stageHeight – RADIUS)
{
ball.y = stage.stageHeight – RADIUS;
ySpeed = 0;
}
else
{
ball.y += ySpeed;
}
}

function accUpdateHandler(event:AccelerometerEvent):void
{
xSpeed += event.accelerationX * 2;
ySpeed -= event.accelerationY * 2;

txtX.text = new String(event.accelerationX);
txtY.text = new String(event.accelerationY);
txtZ.text = new String(event.accelerationZ);
}
[/ACTIONSCRIPT3]
Now just ask to test the emulator in Adobe Device Central CS5, we have something like this:

Adobe Device Central CS5

DOWNLOAD SOURCE
To learn more:
http://help.adobe.com/en_US/flashlite/dev/4/index.html

Was this article helpful? feel free to make a donation and help keep the blog in the air
ActionScript 3.0, Flash, Flash Lite, Nokia

1 comment


  1. Pingback: Using accelerometer with AIR for Android | Leonardo França

Leave a Reply