2015-04-08 65 views
0

創造更多的同樣的功能我嘗試做一些功能在for循環的方法,但它說:「符號‘myInstanceMovieClip’,層‘爲’,框架31,第29行1084:語法錯誤:leftparen期待在leftbracket之前「。 這裏我的代碼:AS3,在for循環mehod

for(var i:int=0; i < btnhairmc.length; i++){ 
    btnhairmc[i].buttonMode=true; 
    btnhairmc[i].addEventListener(MouseEvent.CLICK, goPlay[i]); 
    function goPlay[i](m:MouseEvent):void{ 
     trace("btnhair[i]_mc <= clicked"); 
    } 
} 

哪裏是我的錯嗎?

回答

1

函數名稱:goPlay[i]是無效的,應該只有goPlay

你可以調整你的代碼:

for (var i:int=0; i < btnhairmc.length; i++) { 
    btnhairmc[i].buttonMode=true; 
    btnhairmc[i].addEventListener(MouseEvent.CLICK, goPlay); 
    function goPlay(m:MouseEvent):void { 
     trace("btnhair["+i+"]_mc <= clicked"); 
    } 
} 

或者

for (var i: int = 0; i < btnhairmc.length; i++) { 
    btnhairmc[i].buttonMode = true; 
    btnhairmc[i].addEventListener(MouseEvent.CLICK, function (event: MouseEvent): void { 
     trace("btnhair[" + i + "]_mc <= clicked"); 
    }); 

} 
+0

感謝您的幫助,它的工作原理..:D –

1

你可以這樣做:

for (var i:int=0; i < btnhairmc.length; i++) { 
    btnhairmc[i].buttonMode=true; 
    btnhairmc[i].name="button-"+i; 
    btnhairmc[i].addEventListener(MouseEvent.CLICK, goPlay); 
} 

function goPlay(e:MouseEvent) : void { 
    trace(e.target.name); 
    //or maybe you need 
    trace(e.currentTarget.name); 
    trace('Button' + e.target.name + 'was clicked!'); 
} 

隨着e.targete.currentTarget你就會知道哪個按鈕被按下

希望這有助於。