Loader Event Complete function only being called once
So, I'm trying to load resources, add them to a dictionary, and have a drawing method search through that dictionary and draw based on certain predicates. I have a function that iterates through an Vector
of String
s, calling on an instance of Loader
to load them instantiated as a URLRequest
.
private function loadImages(urls:Vector.<String>):void
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,ioErrorHandler);
var loadResource:Function = function(item:String,index:int,vector:Vector.<String>):void
{
loader.load(new URLRequest(item));
}
urls.forEach(loadResource);
}
I then have an event handler that's handling the load completion, calling a drawing method that finds the Loaded bitmap in a Dictionary
.
public function completeHandler(event:Event):void
{
var loader:Loader = Loader(event.target.loader);
var bm:BitmapData = new BitmapData(loader.width,loader.height,false);
bm.draw(loader,new Matrix());
this.bmDict[loader.contentLoaderInfo.url] = bm;
trace("complete handler" + loader.contentLoaderInfo.url);
trace(this.bmDict[loader.contentLoaderInfo.url]);
this.drawSprite(loader.contentLoaderInfo.url);
}
My drawsprite function public function drawSprite(resourceUrl:String):void { var drawFunct:Function = function(spr:Dictionary,index:int,vector:Vector.):void {
var sprRen:SpriteRenderer = new SpriteRenderer();
trace(resourceUrl.search("blue"));
trace(resourceUrl.search("drkOrange"));
trace(resourceUrl.search("green"));
trace(resourceUrl.search("ltblue"));
trace(resourceUrl.search("orange"));
trace(resourceUrl.search("pink"));
trace(resourceUrl.search("purple"));
trace(resourceUrl.search("red"));
trace(resourceUrl.search("yellow"));
trace(spr.color);
开发者_运维百科
if((resourceUrl.search("blue") != -1) && (spr["color"] == 1)) {
var bigBm:BitmapData = sprRen.renderType(spr["type"],bmDict[resourceUrl]);
spr["sprite"].graphics.beginBitmapFill(bigBm,null,true,false);
spr["sprite"].graphics.drawRect(0,0,BIG_SPRITE_SCALE,BIG_SPRITE_SCALE);
spr["sprite"].graphics.endFill();
} else if ((resourceUrl.search("drkOrange") != -1) && (spr["color"] == 2)){
var bigBm:BitmapData = sprRen.renderType(spr["type"],bmDict[resourceUrl]);
spr["sprite"].graphics.beginBitmapFill(bigBm,null,true,false);
spr["sprite"].graphics.drawRect(0,0,BIG_SPRITE_SCALE,BIG_SPRITE_SCALE);
spr["sprite"].graphics.endFill();
} else if ((resourceUrl.search("green") != -1) && (spr["color"] == 3)){
var bigBm:BitmapData = sprRen.renderType(spr["type"],bmDict[resourceUrl]);
spr["sprite"].graphics.beginBitmapFill(bigBm,null,true,false);
spr["sprite"].graphics.drawRect(0,0,BIG_SPRITE_SCALE,BIG_SPRITE_SCALE);
spr["sprite"].graphics.endFill();
} else if ((resourceUrl.search("ltblue") != -1) && (spr["color"] == 4)){
var bigBm:BitmapData = sprRen.renderType(spr["type"],bmDict[resourceUrl]);
spr["sprite"].graphics.beginBitmapFill(bigBm,null,true,false);
spr["sprite"].graphics.drawRect(0,0,BIG_SPRITE_SCALE,BIG_SPRITE_SCALE);
spr["sprite"].graphics.endFill();
} else if ((resourceUrl.search("orange") != -1) && (spr["color"] == 5)){
var bigBm:BitmapData = sprRen.renderType(spr["type"],bmDict[resourceUrl]);
spr["sprite"].graphics.beginBitmapFill(bigBm,null,true,false);
spr["sprite"].graphics.drawRect(0,0,BIG_SPRITE_SCALE,BIG_SPRITE_SCALE);
spr["sprite"].graphics.endFill();
} else if ((resourceUrl.search("pink") != -1) && (spr["color"] == 6)){
var bigBm:BitmapData = sprRen.renderType(spr["type"],bmDict[resourceUrl]);
spr["sprite"].graphics.beginBitmapFill(bigBm,null,true,false);
spr["sprite"].graphics.drawRect(0,0,BIG_SPRITE_SCALE,BIG_SPRITE_SCALE);
spr["sprite"].graphics.endFill();
} else if ((resourceUrl.search("purple") != -1) && (spr["color"] == 7)){
var bigBm:BitmapData = sprRen.renderType(spr["type"],bmDict[resourceUrl]);
spr["sprite"].graphics.beginBitmapFill(bigBm,null,true,false);
spr["sprite"].graphics.drawRect(0,0,BIG_SPRITE_SCALE,BIG_SPRITE_SCALE);
spr["sprite"].graphics.endFill();
} else if ((resourceUrl.search("red") != -1) && (spr["color"] == 8)){
var bigBm:BitmapData = sprRen.renderType(spr["type"],bmDict[resourceUrl]);
spr["sprite"].graphics.beginBitmapFill(bigBm,null,true,false);
spr["sprite"].graphics.drawRect(0,0,BIG_SPRITE_SCALE,BIG_SPRITE_SCALE);
spr["sprite"].graphics.endFill();
} else if ((resourceUrl.search("yellow") != -1) && (spr["color"] == 9)){
var bigBm:BitmapData = sprRen.renderType(spr["type"],bmDict[resourceUrl]);
spr["sprite"].graphics.beginBitmapFill(bigBm,null,true,false);
spr["sprite"].graphics.drawRect(0,0,BIG_SPRITE_SCALE,BIG_SPRITE_SCALE);
spr["sprite"].graphics.endFill();
}
}
sprites.forEach(drawFunct);
}
So the issue is: Only the first images in my Vector
are drawing. Upon further inspection, I found that the completeHandler was only being called once (I put a trace in the complete handler to check). However, the Loader
is invoking load everytime the loop iterates. I tried instantiating separate loaders for each resource, just to see if it would work, but I had no luck with that. Do I need to make separate loaders and event handlers? Or am I just not using Loader correctly?
You are using only one loader to load multiple files in the same time, it can't work, a Loader
can handle only one file at the same time.
So you need one loader by resource or load resources one by one if you want to use only one loader.
精彩评论