所以我正在尝试加载精灵表。我已将它嵌入到.swf中,但是当我实例化一个类并调用一个从sprite表开始剪切的函数时,我收到错误:
“参数sourceBitmapData必须为非null。”
我知道发生的事情是在实际加载精灵表图像之前调用代码,但我不知道如何解决这个问题,因为我嵌入而不是使用加载器。我假设我需要使用事件监听器,但是如何使用嵌入工作呢?到目前为止,这是我的代码:
在主公共课程中:
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类中,我调用了一个调用copyPixels()的drawTile函数,其中错误来自:
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
答案 0 :(得分:0)
这不是一个加载问题,因为您将png文件嵌入到swf中,所有图像加载都是在加载swf时完成的。
这只是Flash IDE编译器的问题...使用Flex编译器时,这不是必需的......
尝试:
private var sheet:BitmapData = new sheetClass(0,0);
将您的SpriteSheet类更改为直接接受bitmapData