Online Book Reader

Home Category

Developing Android Applications with Adobe AIR [84]

By Root 2586 0

var toggleCache:Boolean = true;

const MAX:int = 200;

var boundsX:int;

var boundsY:int;

var myMatrix:Matrix;

container = new Vector.;

boundsX = stage.stageWidth;

boundsY = stage.stageHeight;

Create the Matrix only once and apply it to all the MovieClips:

myMatrix = new Matrix();

for (var i:int = 0; i < MAX; i++) {

var temp:MovieClip = createCircle();

container[i] = temp;

addChild(temp);

}

Animate all the MovieClips on EnterFrame:

stage.addEventListener(Event.ENTER_FRAME, move);

Create a square-shaped MovieClip. Give it a random alpha, scale, and direction:

function createCircle():MovieClip {

var mc:MovieClip = new MovieClip();

mc.graphics.beginFill(Math.random()*0x09FFFF, Math.random()*1);

mc.graphics.drawRect(-30, -30, 60, 60);

mc.graphics.endFill();

mc.x = Math.random()*stage.stageWidth;

mc.y = Math.random()*stage.stageHeight;

var scale:Number = Math.random()*1;

mc.scaleX = mc.scaleY = scale;

mc.dir = 1;

// cache it for transformation

mc.cacheAsBitmap = toggleCache;

mc.cacheAsBitmapMatrix = myMatrix;

return mc;

}

Scale and rotate individual MovieClips:

function move(event:Event):void {

for (var i:int = 0; i < MAX; i++) {

var mc:MovieClip = container[i];

mc.scaleX += 0.05*mc.dir;

mc.scaleY += 0.05*mc.dir;

mc.rotation += 5*mc.dir;

if (mc.scaleX < 0.05 || mc.scaleX > 1.0) {

mc.dir *= -1;

}

}

}

You should notice a great improvement in tweening animation, even with the large number of objects.

In an effort to preserve memory, all the objects share a single Matrix instance. There is no need to create a unique matrix per object because, once assigned, the Matrix will not be modified.

WARNING

Do not create a new matrix to rotate or scale your object. If you do, it will invalidate your pixel buffer and you will lose the performance benefit. Instead, just modify the display object’s scale and rotation properties as in the example.

To use a different initial matrix than the identity matrix, create it and then apply it to your display object. We will discuss matrices later in this chapter.

In our example, the cacheAsBitmapMatrix property is applied to objects in a flat display list. Caching becomes a more meticulous exercise when dealing with the display list hierarchy. We will go over this in the next section.

The Display List

The structure of your display list is fundamental in this process for three reasons: memory consumption, tree traversal, and node hierarchy.

Memory Consumption


Memory consumption is the trade-off for better performance in GPU rendering because every off-screen bitmap uses memory. At the same time, mobile development implies less GPU memory caching and RAM.

To get a sense of the memory allocated, you can use the following formula:

// 4 bytes are required to store a single 32 bit pixel

// width and height of the tile created

// anti-aliasing defaults to high or 4 in Android

4 * width * height * anti-aliasFactor

A 10 × 10 image represents 1600 bytes

Be vigilant about saving memory in other areas.

Favor the DisplayObject types that need less memory. If the functionality is sufficient for your application, use a Shape or a Sprite instead of a MovieClip. To determine the size of an object, use the following:

import flash.sampler.*;

var shape:Shape = new Shape();

var sprite:Sprite = new Sprite();

var mc:MovieClip = new MovieClip();

trace(getSize(shape), getSize(sprite), getSize(mc));

// 224, 412 and 448 bytes respectively in the AIR runtime

The process of creating and removing objects has an impact on performance. For display objects, use object pooling, a method whereby you create a defined number of objects up front and recycle them as needed. Instead of deleting them, make them invisible or remove them from the display list until you need to use them again. I will demonstrate object pooling in Chapter 18.

You should give the same attention to other types of objects. If you need to remove objects, remove listeners and references so that they can be garbage-collected and free up precious memory.

Tree Structure

Return Main Page Previous Page Next Page

®Online Book Reader