Re: [問題] 一個簡化程式的問題
[追加]
補上事後寫的AS3 Event Listener Delegtes詳細文章
http://allenchou.net/2012/07/actionscript-3-0-event-listener-delegates/
可以考慮把參數和function reference包到物件裡
這個技巧就是所謂的"委派" (delegate)
//in delegate.as
package
{
public function delegate(func:Function, ...params):Function
{
return new Delegate(func, params).invoke;
}
}
import flash.events.Event;
class Delegate
{
private var func_ :Function;
private var params_ :Array;
public function Delegate(func:Function, ...params)
{
func_ = func;
params_ = params;
}
public function invoke(e:Event):void
{
func_.apply(null, params_);
}
}
//end of Delegate.as
//in time line
function myListener(i:int):void
{
gotoAndPlay(i);
}
for (var i:int = 0; i < 3; ++i)
{
addEventListener
(
MouseEvent.CLICK,
delegate(myListener, i)
);
}
當然,如果你的myListener裡面只有呼叫gotoAndPlay的話
其實根本不用額外宣告myListener這個function
直接傳入gotoAndPlay就可以了
addEventListener
(
MouseEvent.CLICK,
delegate(gotoAndPlay, i);
);
--
Blog http://blog.cjcat.net
Gallery http://cjcat2266.deviantart.com
ptt2 Board CJWorkshop
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 72.5.59.140
※ 編輯: cjcat2266 來自: 72.5.59.140 (07/17 06:57)
→
07/17 06:58, , 1F
07/17 06:58, 1F
※ 編輯: cjcat2266 來自: 72.5.59.140 (07/17 07:16)
推
07/17 12:36, , 2F
07/17 12:36, 2F
推
07/17 13:02, , 3F
07/17 13:02, 3F
推
07/17 13:06, , 4F
07/17 13:06, 4F
→
07/17 13:33, , 5F
07/17 13:33, 5F
→
07/17 13:46, , 6F
07/17 13:46, 6F
→
07/17 13:48, , 7F
07/17 13:48, 7F
→
07/17 13:50, , 8F
07/17 13:50, 8F
→
07/17 14:44, , 9F
07/17 14:44, 9F
→
07/17 14:44, , 10F
07/17 14:44, 10F
→
07/17 14:49, , 11F
07/17 14:49, 11F
→
07/17 14:49, , 12F
07/17 14:49, 12F
→
07/17 14:55, , 13F
07/17 14:55, 13F
→
07/17 15:07, , 14F
07/17 15:07, 14F
→
07/17 15:07, , 15F
07/17 15:07, 15F
→
07/17 15:08, , 16F
07/17 15:08, 16F
→
07/17 15:08, , 17F
07/17 15:08, 17F
→
07/17 15:12, , 18F
07/17 15:12, 18F
→
07/17 15:20, , 19F
07/17 15:20, 19F
→
07/17 15:21, , 20F
07/17 15:21, 20F
→
07/17 15:23, , 21F
07/17 15:23, 21F
討論串 (同標題文章)
Flash 近期熱門文章
PTT數位生活區 即時熱門文章