0
我想複製位圖數據的像素。但我不斷收到以下錯誤參數sourceBitmapData必須爲非空
Parameter sourceBitmapData must be non-null.
它發生在方法drawImage中。確切地說,我的跟蹤說「獲得畫布數據」。
canvasData.copyPixels(image,tileRect,tilePoint);
下面是我的代碼
package com.objects {
import flash.display.Sprite;
import flash.display.BitmapData;
import flash.display.Bitmap;
import flash.geom.Point;
import flash.geom.Rectangle;
import flash.display.Loader;
import flash.net.URLRequest;
import flash.net.*;
import flash.events.*;
import flash.display.LoaderInfo;
public class gameObject extends Sprite {
protected var goWidth:Number;
protected var goHeight:Number;
protected var image:BitmapData;
protected var canvas:Bitmap;
public function gameObject():void
{
init();
}
private function init():void
{
}
public function loadImage(imageDir:String):void
{
trace("loadimage");
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,imageComplete);
loader.load(new URLRequest(imageDir));
}
private function imageComplete(e:Event):void {
var loader:LoaderInfo = LoaderInfo(e.target);
image = Bitmap(loader.content).bitmapData;
trace("imagecomplete");
}
public function drawImage(xPoint:Number = 0, yPoint:Number = 0, tWidth:Number = 0, tHeight:Number = 0):void
{
var tilePoint:Point = new Point(0,0);
var tileRect = new Rectangle(xPoint,yPoint,tWidth,tHeight);
var canvasData:BitmapData = new BitmapData(tWidth,tHeight);
trace("got canvas data");
canvasData.copyPixels(image,tileRect,tilePoint);
trace("copied pixels");
canvas = new Bitmap(canvasData);
}
}
}
以下是我用來調用類
balls = new Array();
balls[0] = new gameObject();
balls[0].loadImage("com/images/ball.gif");
balls[0].drawImage(0,0,15,15);
任何有線索?