Re: [問題] 用for做function
※ 引述《hirabbitt (兔子)》之銘言:
: for(i=0;i<5;i++){
: questImage=new canGet();
: questImage.addEventListener(MouseEvent.CLICK,function(e:MouseEvent):void{
: var tempi:int=i;
: trace(i+" "+tempi); //實際在按的時候i和tempi印出來都是1 但我想要0~4
: });
: }
: 我有想過把i傳值傳進去
: 但是不知道MouseEvent.CLICK的function是否可以直接function(e:MouseEvent,temp:int)
: 感謝
用 closure
步驟1 : 在場景上放五個任意的元件
步驟2 : 在影格內寫入下面的程式
function closure(i:int):Function{
return function(e:MouseEvent):void{
trace(i);
};
}
for(var i:int = 0 ; i < this.numChildren ; i++)
this.getChildAt(i).addEventListener(MouseEvent.CLICK, closure(i));
步驟3 : CTRL + ENTER 執行看結果
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 140.137.198.18
※ 編輯: etrexetrex 來自: 140.137.198.18 (12/12 09:56)
討論串 (同標題文章)
本文引述了以下文章的的內容:
完整討論串 (本文為第 2 之 2 篇):
Flash 近期熱門文章
PTT數位生活區 即時熱門文章