您應該刪除事件偵聽器時,它的無需要的時間更長,否則會冒內存泄漏的風險。一個匿名函數可以使這個變得困難,儘管你可以用arguments.callee來完成它。
bar.addEventListener(Event.ENTER_FRAME, function(e:Event) {
var b:MovieClip = MovieClip(e.currentTarget);
if(b.currentFrame == mp.getPopularity()){
b.stop();
b.removeEventListener(e.type, arguments.callee);
// ^^ this may work to remove your anonymous listener.
}
});
不過還有另外一種方法可以解決這個問題。 mp.getPopularity()是否經常更改?如果它在bar被告知play()後沒有改變,那麼你可以使用addFrameScript。請記住,addFrameScript是0索引的,因此向第1幀添加腳本意味着您必須傳遞0 ...因此,如果在mp.getPopularity()上發生操作,則必須通過mp.getPopularity() - 1.
var framescript:Function = function():void{
bar.stop();
bar.addFrameScript(bar.currentFrame-1, null);
// ^^ nulling the framescript after
// ^^ it is no longer needed.
}
bar.stop(); // Generally a good idea to call stop before calling addFrameScript
bar.addFrameScript(mp.getPopularity() - 1, framescript);
bar.gotoAndPlay(1); // or wherever it needs to start from.
這是一個更精確的解決方案,但你必須要記得清理你的framescript大功告成後,如果您打算用不同的mp.getPopularity後使用同樣的欄實例()值。
你可以試試'addFrameScript()' - 它沒有記錄,但它可能會工作:http://www.kirupa.com/forum/showthread.php?223798-ActionScript-3-Tip-of-the-Day/page22&p = 2098268#post2098268 – divillysausages 2012-03-26 21:05:00