AS3 Sprite Sheets

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

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

+3
source share
2 answers

The short answer is that you want to use BitmapData.copyPixels () to copy only a small section from your original sprite sheet into your sprite screen, which is actually on the screen.

Sort of:

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 useful - they helped me when I found out about this:

+10

32x32 .

- ():

var spriteMask:Sprite = new Sprite();
spriteMask.graphics.drawRect(0,0,32,32);
spriteSheetContainer.mask = spriteMask;

function render():void {    // this function is on the container of the sprite sheet (spriteSheetContainer in this example)
    // run offsetX & Y iteration logic.  I would assume something that uses a frame counter, modulus, and the sprite layout grid dimensions
    _spriteSheet.x = offsetX;    // move the sprite around under the mask
    _spriteSheet.y = offsetY;
}

, , .

+1

Source: https://habr.com/ru/post/1713774/


All Articles