首先,你有同名使用兩次(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)。他們需要不同的標識符(如果您願意的話,可以使用clickFunc1
和clickFunc2
)。這是我的建議解決方案:
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();
}
我已經嘗試過,它產生和額外的「1120:訪問未定義的屬性clickFunc」本身的錯誤 – ed209 2010-01-06 13:29:37