2010-01-13 187 views
0

我無法連接事件監聽器,以我的數組中元素的數組元素,任何幫助/建議將是偉大的..感謝閱讀AS3附加事件監聽器

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(new URLRequest(url)); 
return loader; 
} 
+0

我的不好,我應該更具體一些,我想用特定的eventlistener將數組內的每個「jpg」作爲目標。當數組由URLS組成時,甚至有可能嗎? – steven 2010-01-13 04:42:22

回答

0
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. 
} 
+0

我的不好,我應該更具體一些,我想用特定的eventlistener將數組中的每個「jpg」作爲目標。當數組由URLS組成時,甚至有可能嗎? – steven 2010-01-13 04:43:34

2

我想你不需要多一個數組。您可以使用loaderInfo的url屬性來查找URL。

function onLoad(e:Event):void { 
    var url:String = e.traget.url; 
    var index:Number = urls.indexOf(url); 
}

+0

你,對。 +1 – Amarghosh 2010-01-13 10:47:03