AS3 精灵表 [英] AS3 Sprite Sheets
问题描述
我有一张图片 mySprite.png.图像是 32x32 像素精灵的 5x5 网格.此图片已加载到项目库中.
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.
假设我在一个类中有一个 render() 函数,这个类如何从这个精灵表资源中将自己绘制为单个精灵?
Assuming I have a render() function inside a class, how would this class draw itself as a single sprite from this sprite sheet resource?
推荐答案
简短的回答是您需要使用 BitmapData.copyPixels() 将源精灵表中的一小部分复制到显示器实际出现在屏幕上的精灵.
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.
类似于:
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
这篇关于AS3 精灵表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!