AS3 Sprite Sheets

后端 未结 2 1522
离开以前
离开以前 2020-12-15 02:02

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() functio

相关标签:
2条回答
  • 2020-12-15 02:30

    Another possible method would be to put a 32x32 mask over the sheet and just move the sheet around.

    It would work something like (pseudo-code):

    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;
    }
    

    It's crucial to have the mask on a container of the sprite sheet and not the sprite sheet itself, so that you can move the sprite sheet independent of the mask.

    0 讨论(0)
  • 2020-12-15 02:57

    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
    0 讨论(0)
提交回复
热议问题