所以我正在尝试加载一个精灵表。我已经将它嵌入到 .swf 中,但是当我实例化一个类并调用一个从精灵表开始剪辑的函数时,我得到了错误:
“参数 sourceBitmapData 必须为非空。”
我知道正在发生的事情是在实际加载精灵表图像之前调用了代码,但我不确定如何解决这个问题,因为我是嵌入而不是使用加载器。我假设我需要使用事件侦听器,但是如何使用嵌入来实现呢?到目前为止,这是我的代码:
在主要公共课程中:
private var currentSprite:SpriteSheet;
[Embed(source='../assets/images/sprite_sheet_1.png')]
private var sheetClass:Class;
private var sheet:Bitmap = new sheetClass();
然后在Main函数中:
currentSprite = new SpriteSheet( sheet, 25, 25 );
addChild(currentSprite);
SpriteSheet 函数的设置如下:
public function SpriteSheet(tileSheetBitmap:Bitmap, width:Number = 25, height:Number = 25) {
tileSheetBitmapData = tileSheetBitmap.bitmapData;
tileWidth = width;
tileHeight = height;
rowLength = int(tileSheetBitmap.width / width );
tileRectangle = new Rectangle( 0, 0, tileWidth, tileHeight );
tilePoint = new Point( 0, 0 );
canvasBitmapData = new BitmapData( tileWidth, tileHeight, true );
var canvasBitmap:Bitmap = new Bitmap(canvasBitmapData);
addChild(canvasBitmap);
drawTile(0);
addEventListener(Event.REMOVED_FROM_STAGE, remove);
}
在我的 SpriteSheet 类中,我调用了一个 drawTile 函数,该函数调用了 copyPixels(),错误来自:
canvasBitmapData.copyPixels(tileSheetBitmapData, tileRectangle, tilePoint);
我知道这是很多代码,但我觉得可能有一个简单的解决方案。任何反馈将不胜感激。谢谢!
编辑:这是 SpriteSheet 类:
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Sprite;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.events.Event;
public class SpriteSheet extends Sprite {
public var tileSheetBitmapData:BitmapData;
private var canvasBitmapData:BitmapData;
private var tileWidth:int;
private var tileHeight:int;
private var rowLength:int;
private var tileRectangle:Rectangle;
private var tilePoint:Point;
public function SpriteSheet(tileSheetBitmap:Bitmap, width:Number = 25, height:Number = 25) {
tileSheetBitmapData = tileSheetBitmap.bitmapData;
if(tileSheetBitmap != null ) {
trace("tileSheetBitmap is not null in SpriteSheet");
}
if( tileSheetBitmapData != null ){
trace("tileSheetBitmapData is not null in SpriteSheet");
}
tileWidth = width;
tileHeight = height;
rowLength = int(tileSheetBitmap.width / width );
tileRectangle = new Rectangle( 0, 0, tileWidth, tileHeight );
tilePoint = new Point( 0, 0 );
canvasBitmapData = new BitmapData( tileWidth, tileHeight, true );
var canvasBitmap:Bitmap = new Bitmap(canvasBitmapData);
addChild(canvasBitmap);
drawTile(0);
addEventListener(Event.REMOVED_FROM_STAGE, remove);
} //end of SpriteSheet
public function drawTile(tileNumber:int):BitmapData {
trace("Reached drawTile");
tileRectangle.x = int( (tileNumber % rowLength) ) * tileWidth;
tileRectangle.y = int( (tileNumber / rowLength) ) * tileHeight;
canvasBitmapData.copyPixels(tileSheetBitmapData, tileRectangle, tilePoint);
trace("Reached past copyPixels");
return canvasBitmapData.clone();
} //end of drawTile
public function tileBoard(boardIndex:Array):BitmapData {
var wide:int = boardIndex[0].length;
var tall:int = boardIndex.length;
canvasBitmapData = new BitmapData( (tileWidth * wide), (tileHeight * tall), true);
var boardCanvas:Bitmap = Bitmap(getChildAt(0));
boardCanvas.bitmapData = canvasBitmapData;
tileRectangle = new Rectangle( 0, 0, (tileWidth * wide), (tileHeight * tall) );
for( var i:int = 0; i < wide; i++ ) {
for( var j:int = 0; j < tall; j++ ) {
tilePoint = new Point((tileWidth * i), (tileHeight * j) );
drawTile(boardIndex[j][i]);
}
}
return canvasBitmapData.clone();
} //end tileBoard
public function remove(e:Event):void {
tileSheetBitmapData.dispose();
canvasBitmapData.dispose();
} //end remove
} //end class SpriteSheet
} //end package