AS3 精灵表 [英] AS3 Sprite Sheets

查看:25
本文介绍了AS3 精灵表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一张图片 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:

这篇关于AS3 精灵表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆