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?
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With