Skip navigation
Currently Being Moderated

Best way to handle Sprite animation

Jul 11, 2012 6:47 PM

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[i] );
     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

 
Replies

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points