2010-01-06 71 views
10
up.addEventListener(MouseEvent.CLICK, 
    function clickFunc(event:MouseEvent):void 
    { 
     revealSpinner(event,51.42,1,spinner); 
     event.currentTarget.removeEventListener(event.type, arguments.callee); 
     autoTimer.stop(); 
    }, 
    false, 0, true); 
down.addEventListener(MouseEvent.CLICK, 
    function clickFunc(event:MouseEvent):void 
    { 
     revealSpinner(event,51.42,-1,spinner); 
     event.currentTarget.removeEventListener(event.type, arguments.callee); 
     autoTimer.stop(); 
    }, 
    false, 0, true); 

上面的代碼將一個監聽器添加到一對MC中。最初的方法是匿名的,但我已經命名爲clickFunc(),以便在我的刪除偵聽器中嘗試引用它們。刪除一個在Actionscript 3中有一個匿名函數的監聽器

這是我的刪除偵聽器代碼。這兩個片段都有獨立的功能。 add方法在remove方法之前被調用。

up.removeEventListener(MouseEvent.CLICK, clickFunc); 
down.removeEventListener(MouseEvent.CLICK, clickFunc); 

只要我發佈的電影,我得到這個錯誤:

1120: Access of undefined property clickFunc. 

回答

0

試試這個:

up.addEventListener(MouseEvent.CLICK, 
    clickFunc = function(event:MouseEvent):void 
    { 
     revealSpinner(event,51.42,1,spinner); 
     event.currentTarget.removeEventListener(event.type, arguments.callee); 
     autoTimer.stop(); 
    }, 
    false, 0, true); 

down.addEventListener(MouseEvent.CLICK, 
    clickFunc = function(event:MouseEvent):void 
    { 
     revealSpinner(event,51.42,-1,spinner); 
     event.currentTarget.removeEventListener(event.type, arguments.callee); 
     autoTimer.stop(); 
    }, 
    false, 0, true); 
+0

我已經嘗試過,它產生和額外的「1120:訪問未定義的屬性clickFunc」本身的錯誤 – ed209 2010-01-06 13:29:37

2

這一個應該工作...只是刪除了Methode名稱...

up.addEventListener(MouseEvent.CLICK, 
     function(event:MouseEvent):void 
     { 
      revealSpinner(event,51.42,1,spinner); 
      event.currentTarget.removeEventListener(event.type, arguments.callee); 
      autoTimer.stop(); 
     }, 
     false, 0, true); 
    down.addEventListener(MouseEvent.CLICK, 
     function(event:MouseEvent):void 
     { 
      revealSpinner(event,51.42,-1,spinner); 
      event.currentTarget.removeEventListener(event.type, arguments.callee); 
      autoTimer.stop(); 
     }, 
     false, 0, true); 
+0

是的,這工作,但我需要從「向上」和「向下」刪除事件監聽器無論哪一個你點擊。你的建議將刪除需要事件調用removeEventListener(),因爲它是CLICK行動的一部分。我需要從添加偵聽器的外部刪除偵聽器。 – ed209 2010-01-06 13:51:27

+2

我能想到的唯一方法是將兩個功能分配給本地變量,用它們來增加聽衆,並返回這兩個增值經銷商爲對象,並存儲在一個全局變量對象。現在,請從使用'globalObj.upFunction'和'globalObj.downFunction'兩種功能刪除監聽器。但在你開始編寫代碼之前,我強烈建議你考慮使用實例方法而不是匿名/內部函數。 – Amarghosh 2010-01-06 14:50:15

5

首先,你有同名使用兩次(clickFunc),無法推斷您在調用removeEventListener時引用了哪一個。如果您需要引用方法(比如說,從事件中刪除它們),他們不能匿名

function foo() { 
    var clickFunc: Function; 
    up.addEventListener(MouseEvent.CLICK, 
     clickFunc = function (event:MouseEvent):void 
     { 
      revealSpinner(event,51.42,1,spinner); 
      event.currentTarget.removeEventListener(event.type, arguments.callee); 
      autoTimer.stop(); 
     }, 
     false, 0, true); 

    // 'clickFunc' available here, so this is possible: 
    up.removeEventListener(MouseEvent.CLICK, clickFunc); 
} 

function bar() { 
    // 'clickFunc' is not available here, so this is not possible: 
    up.removeEventListener(MouseEvent.CLICK, clickFunc); 
    // 1120: Access of undefined property clickFunc 
} 

:所有第二,clickFunc只會在聲明在函數訪問。如果你需要從幾種方法中引用它們,那麼它們不應該是一種方法的本地方法(上面例子中的foo)。他們需要不同的標識符(如果您願意的話,可以使用clickFunc1clickFunc2)。這是我的建議解決方案:

private function addHandlers(): void 
{ 
    up.addEventListener(MouseEvent.CLICK, upClickHandler, false, 0, true); 
    down.addEventListener(MouseEvent.CLICK, downClickHandler, false, 0, true); 
} 

private function removeHandlers(): void 
{ 
    up.removeEventListener(MouseEvent.CLICK, upClickHandler); 
    down.removeEventListener(MouseEvent.CLICK, downClickHandler); 
} 

private function upClickHandler(event:MouseEvent):void 
{ 
    revealSpinner(event,51.42,1,spinner); 
    event.currentTarget.removeEventListener(event.type, arguments.callee); 
    autoTimer.stop(); 
} 

private function downClickHandler(event:MouseEvent):void 
{ 
    revealSpinner(event,51.42,-1,spinner); 
    event.currentTarget.removeEventListener(event.type, arguments.callee); 
    autoTimer.stop(); 
} 

當然,如果像在你的榜樣,這些方法是相同的,你只能使用一個:

private function addHandlers(): void 
{ 
    up.addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true); 
    down.addEventListener(MouseEvent.CLICK, clickHandler, false, 0, true); 
} 

private function removeHandlers(): void 
{ 
    up.removeEventListener(MouseEvent.CLICK, clickHandler); 
    down.removeEventListener(MouseEvent.CLICK, clickHandler); 
} 

private function clickHandler(event:MouseEvent):void 
{ 
    revealSpinner(event,51.42,-1,spinner); 
    event.currentTarget.removeEventListener(event.type, arguments.callee); 
    autoTimer.stop(); 
} 
3

確定有幾個方法,你可以做這個。這是我認爲是你想要的第一種方式。

var upAnon:Function; 
var downAnon:Function; 

up.addEventListener(MouseEvent.CLICK, 
    upAnon = function (event:MouseEvent):void 
    { 
     revealSpinner(event,51.42,1,spinner); 
     event.currentTarget.removeEventListener(event.type, arguments.callee); 
     autoTimer.stop(); 
    }, 
    false, 0, true); 
down.addEventListener(MouseEvent.CLICK, 
    downAnon = function (event:MouseEvent):void 
    { 
     revealSpinner(event,51.42,-1,spinner); 
     event.currentTarget.removeEventListener(event.type, arguments.callee); 
     autoTimer.stop(); 
    }, 
    false, 0, true); 

up.removeEventListener(MouseEvent.CLICK, upAnon); 
down.removeEventListener(MouseEvent.CLICK, downAnon); 

這是另一種方式,獲得與第一種相同的效果和功能。只是更清潔,通常是標準做法。

function upFunction(event.MouseEvent):void { 
    revealSpinner(event,51.42,1,spinner); 
    event.currentTarget.removeEventListener(event.type, arguments.callee); 
    autoTimer.stop(); 
} 
function downFunction(event.MouseEvent):void { 
    revealSpinner(event,51.42,-1,spinner); 
    event.currentTarget.removeEventListener(event.type, arguments.callee); 
    autoTimer.stop(); 
} 

up.addEventListener(MouseEvent.CLICK, upFunction, false, 0, true); 
down.addEventListener(MouseEvent.CLICK, downFunction, false, 0, true); 

up.removeEventListener(MouseEvent.CLICK, upFunction); 
down.removeEventListener(MouseEvent.CLICK, downFunction); 

,您將使用第一種原因,就是如果你創建事件處理函數內,需要使用該函數定義的變量,你的匿名函數裏面。例如,在這裏,我訪問anon函數內的var countMe,並在mouse_down上增加它,它會增加,直到mouse_up被激活。所以每次點擊時,它都會從0開始計數,直到鼠標點亮,然後重新開始。

var anonFunc:Function; 

function doThisStuff(event:MouseEvent):void { 
    var countMe = 0; 

    stage.addEventListener(Event.ENTER_FRAME, 
     anonFunc = function (e:Event) { 
      countMe++; 
      trace(countMe); 
     } 
    ); 
} 

function stopCounting(event:MouseEvent):void { 
    stage.removeEventListener(Event.ENTER_FRAME, anonFunc); 
} 

stage.addEventListener(MouseEvent.MOUSE_DOWN, doThisStuff); 
stage.addEventListener(MouseEvent.MOUSE_UP, stopCounting); 
2

您正在創建具有弱引用的事件處理函數(爲最後一個參數傳遞true)。正如你可能已經知道的那樣,一個弱引用不會阻止垃圾收集器收集你的函數。然而,除非你在你定義的函數範圍之外保留對你的事件處理函數的引用(看起來你並不像),否則處理程序將只有一個弱引用來保持它們的活性,所以儘快垃圾收集器運行,你的處理程序不管了。底線是,你的事件處理程序不喜歡看一次垃圾收集運行,因此不必擔心刪除處理程序是有些無意義,他們會火。無論你是否願意,他們都會自行消失。

你需要保持在一個範圍內對這些事件處理程序的參考,這將仍然存在時,他們應該解僱。一旦你完成了,你應該能夠使用這些引用來刪除它們。