我正在处理我正在研究的课程的问题。我目前将图像作为位图加载并将其数据存储到regState:BitmapData中,以便稍后我可以创建该图像的新实例。当我测试我是否可以在以后使用newBitmapIntance()函数使用加载的数据时,它表示regState为null。我很遗憾为什么会这样,因为它在我的loadContent()函数中创建自己的实例是完美无瑕的。我能用什么方法成功保存到我加载的bitmapdata以供以后使用?
到目前为止类参考:
package {
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.display.Bitmap;
import flash.display.BitmapData;
public class MyObj extends MovieClip
{
private var regState:BitmapData;
public function MyObj()
{
}
public function loadImage(url:String):void
{
var urlRequest:URLRequest = new URLRequest(url);
var imgLdr:Loader = new Loader();
imgLdr.load(urlRequest);
imgLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, loadContent);
}
private function setRegState(temp:BitmapData):void
{
this.regState = temp.clone();
}
public function getRegState():BitmapData
{
return this.regState;
}
private function loadContent(event:Event)
{
var temp:Bitmap = event.target.content as Bitmap;
this.setRegState(temp.bitmapData);
this.addChild(newBitmapIntance());
}
public function newBitmapIntance():Bitmap
{
var temp:Bitmap = new Bitmap(this.getRegState());
return temp;
}
}
答案 0 :(得分:0)
重构了MyObj:
package
{
import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.display.MovieClip;
import flash.events.Event;
import flash.net.URLRequest;
public class MyObj extends MovieClip
{
private var _bitmapData:BitmapData;
public function loadImage(url:String):void
{
var urlRequest:URLRequest = new URLRequest(url);
var imgLdr:Loader = new Loader();
imgLdr.load(urlRequest);
imgLdr.contentLoaderInfo.addEventListener(Event.COMPLETE, loadContent);
}
public function clone():MovieClip
{
var newClip:MovieClip = new MovieClip();
newClip.addChild( new Bitmap( _bitmapData.clone() ) );
return newClip;
}
public function MyObj()
{
}
private function loadContent(event:Event):void
{
this._bitmapData = Bitmap(event.target.content).bitmapData;
this.dispatchEvent( new Event( "imageLoaded" ) );
}
}
}
从另一个班级使用它:
package
{
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
public class Test extends Sprite
{
private var _factory:MyObj;
public function Test()
{
_factory = new MyObj();
_factory.loadImage( "test.jpg" );
_factory.addEventListener( "imageLoaded", imageLoaderHandler );
}
private function imageLoaderHandler( e:Event ):void
{
var one:MovieClip = _factory.clone();
this.addChild( one );
var two:MovieClip = _factory.clone();
two.x = 100;
two.y = 100;
this.addChild( two );
}
}
}