AS3 Attaching event listeners to elements of an array
I am having trouble attaching eventListeners to the elements within my array, any help/suggestions would be great.. thanks for reading
var urls:Array=["../showcaseThumbnails/1.jpg",
"../showcaseThumbnails/2.jpg",
"../showcaseThumbnails/3.jpg",
"../showcaseThumbnails/4.jpg",
"../showcaseThumbnails/5.jpg",
"../showcaseThumbnails/6.jpg"];
var loader:Loader;
for (var i:int=0;i<urls.length;i++)
{
loader=startLoading(urls[i]);
loader.y=510;
loader.x=i*54+314;
addChild(loader);
}
function startLoading(url:String):Loader
{
var loader:Loader=new Loader();
loader.load开发者_JS百科(new URLRequest(url));
return loader;
}
I guess you don't need one more array. you can use loaderInfo's url property to find out the URL.
function onLoad(e:Event):void {
var url:String = e.traget.url;
var index:Number = urls.indexOf(url);
}
var urls:Array=["../showcaseThumbnails/1.jpg",
"../showcaseThumbnails/2.jpg",
"../showcaseThumbnails/3.jpg",
"../showcaseThumbnails/4.jpg",
"../showcaseThumbnails/5.jpg",
"../showcaseThumbnails/6.jpg"];
var loaders:Array = [];//array to store loaders
var loader:Loader;
for (var i:int=0;i<urls.length;i++)
{
loader=startLoading(urls[i]);
loaders.push(loader);
loader.y=510;
loader.x=i*54+314;
addChild(loader);
}
function startLoading(url:String):Loader
{
var loader:Loader=new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
loader.load(new URLRequest(url));
return loader;
}
function onLoad(e:Event):void
{
var index:Number = loaders.indexOf(LoaderInfo(e.target).loader);
trace(urls[index]);
//call the corresponding function from here based on index.
}
精彩评论