2009-07-28 117 views
3

我有一個圖像mySprite.png。該圖像是一個32x32像素精靈的5x5網格。該圖像已加載到項目的庫中。AS3雪碧片

假設我在一個類內部有一個render()函數,這個類如何從這個精靈表單資源中將自己繪製爲一個精靈?

回答

10

簡短的回答是,你會希望使用BitmapData.copyPixels()從源精靈表只複製一小部分到實際在屏幕上的顯示精靈。

喜歡的東西:

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

您可能會發現這些鏈接有幫助 - 他們幫助我,當我在學習這一點:

+0

這3個鏈接不起作用 – 2013-05-12 07:56:57

+0

使用http://web.archive.org/ – 2013-08-13 19:13:03

1

另一種可能的方法是將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; 
} 

這是至關重要有精靈表的容器,而不是精靈表本身的面膜,讓你可以移動精靈表獨立於面具。