Add a child inside a newly created instance, inside of a loop in AS3
I am trying to create a gallery where each thumb is housed inside of it's own movie clip that will have more data, but it keeps failing because it won't let me refer to the newly created instance of the movie clip. Below is what I am trying to do.
var xml:XML;
var xmlReq:URLRequest = new URLRequest("xml.xml");
var xmlLoader:URLLoader = new URLLoader();
var imageLoader:Loader;
var vidThumbn:ThumbNail;
var next_y:Number = 0;
for(var i:int = 0; i < xml.downloads.videos.video.length(); i++)
{
vidThumbn = new ThumbNail();
imageLoader = new Loader();
imageLoader.load(开发者_StackOverflow社区new URLRequest(xml.downloads.videos.video[i].ThumbnailImage));
vidThumbn.y = next_y;
vidThumbn.x = 0;
next_y += 117;
imageLoader.name = xml.downloads.videos.video[i].Files[0].File.URL;
videoBox.thumbList.thumbListHolder.addChild(vidThumbn);
videoBox.thumbList.thumbListHolder.vidThumbn.addChild(imageLoader);
}
It dies every time on that last line. How do I refer to that vidThumbn instance so I can add the imageLoader? I don't know what I'm missing. It feels like it should work.
You have to refer to it as vidThumbn, not the extended address....
i think it fails because the imageLoader hasn't loaded the image. Furthermore imageLoader doesnt hold the "data" its iamgeLoader.content
best way: create a "LoaderSprite class" instead of imageLoader = new Loader();
public class LoaderSprite extends Sprite
{
private var _ldr : Loader;
public function LoaderSprite(url : String)
{
_ldr = new Loader();
_ldr.load(new URLRequest(url));
_ldr.contentLoaderInfo.addEventListener(Event.COMPLETE, onComplete);
}
private function onComplete(event : Event) : void
{
//Bitmap(_ldr.content).smoothing = true;
addChild(_ldr.content);
// Fireing your own event
//dispatchEvent(new LoaderSpriteEvent(LoaderSpriteEvent.LOADED));
}
}
精彩评论