Skip to main content
Projectitis
Inspiring
July 12, 2012
Question

Best way to handle Sprite animation

  • July 12, 2012
  • 1 reply
  • 904 views

Hi all,

Comments on overheads (memory/performance) on the following techniques?

These are overly simplified, but you get the idea?

Bitmap.visible

// Assume we have a bunch of sprites already in memory as bitmap data

var spriteData : Vector.<BitmapData>;

// Now we create a sprite for each 'frame' of animation

var bitmaps : Vector.<Bitmap> = new Vector.<Bitmap>();

var bitmap : Bitmap;

for (var i:int=0; i<spriteData.length; i++){

     bitmap = new Bitmap( spriteData );

     addChild(bitmap);

     bitmaps.push(bitmap);

     bitmap.visible = false;

}

// Now when we animate we simply set bitmaps visiblity

public function gotoAndStop( frame : int ):void{

     bitmaps[ currentFrame ].visible = false;

     currentFrame = frame;

     bitmaps[ currentFrame ].visible = true;

}

BitmapData replacement

// Assume we have a bunch of sprites already in memory as bitmap data

var spriteData : Vector.<BitmapData>;

// Now we create a single sprite

var bitmap : Bitmap;

addChild(bitmap);

// Now when we animate we swap bitmap data

public function gotoAndStop( frame : int ):void{

     bitmap.bitmapData = spriteData[ frame ];

}

I haven't performed this test on mobile yet - just wanted to get thoughts first.  Obviously if the animation has a lot of frames then the top one has a huge overhead, but is there a performance hit to swapping the bitmapData of a bitmap?

Cheers,

Peter

This topic has been closed for replies.

1 reply

July 13, 2012

This page has a good review that may be helpful:

http://blog.starnut.com/flash-to-ios-performance-tests/