Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AS3 Sprite Sheets

I have an image mySprite.png. The image is a 5x5 grid of 32x32 px sprites. This image has been loaded into the project's library.

Assuming I have a render() function inside a class, how would this class draw itself as a single sprite from this sprite sheet resource?

like image 621
soundly_typed Avatar asked Jun 01 '26 13:06

soundly_typed


1 Answers

The short answer is that you will want to use BitmapData.copyPixels() to copy only a small section from your source sprite sheet to your display sprite that is actually on the screen.

Something like:

private function DrawSpriteIndex( displayBitmap:Bitmap, spriteSheet:Bitmap, spriteIndex:int ):void {
  var spriteW:int = 32;
  var spriteH:int = 32;
  var sheetW:int = 5;

  displayBitmap.bitmapData.copyPixels(spriteSheet.bitmapData, 
                                       new Rectangle( (spriteIndex % sheetW) * spriteW, Math.floor(spriteIndex / sheetW) * spriteH, 32, 32),
                                       new Point(0,0)
                                      );
}

You may find these links helpful -- they helped me when I was learning this:

  • http://www.8bitrocket.com/2008/07/02/tutorial-as3-the-basics-of-tile-sheet-animation-or-blitting/
  • http://www.8bitrocket.com/newsdisplay.aspx?newspage=13430
  • http://www.8bitrocket.com/newsdisplay.aspx?newspage=14591
  • http://www.8bitrocket.com/newsdisplay.aspx?newspage=15967
like image 58
HanClinto Avatar answered Jun 03 '26 07:06

HanClinto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!