flash - 删除在 Actionscript 3 中具有匿名函数的监听器

标签 flash actionscript-3 events event-handling

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() 以便在我的删除监听器中尝试引用它们。

这是我的删除监听器代码。这两个片段都在不同的功能中。 add 监听器方法在 remove 方法之前调用。
up.removeEventListener(MouseEvent.CLICK, clickFunc );
down.removeEventListener(MouseEvent.CLICK, clickFunc);

一旦我发布电影,我就会收到此错误:
1120: Access of undefined property clickFunc.

最佳答案

首先,您使用了两次相同的名称 ( clickFunc ),无法推断您在调用 removeEventListener 时指的是哪个。其次,clickFunc只能在声明它的函数中访问:

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
}

如果您需要引用这些方法(例如,从事件中删除它们),它们不能是匿名的。如果您需要从多个方法中引用它们,那么它们不应该是一种方法的局部变量(上面示例中的 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();
}

关于flash - 删除在 Actionscript 3 中具有匿名函数的监听器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2013008/

相关文章:

只有一个 Activity 和多个 fragment 的 Android

wcf - PRISM 和 WCF - 他们玩得好吗?

javascript - 使用outerHTML重写html时避免页面刷新

android - setPluginState(WebSettings.PluginState) 已弃用。我应该怎么办? ( WebView 插件)

actionscript-3 - ActionScript3 中循环视频缓冲区

flash as3 动态实例变量名/连接

ios - FlashDevelop - 在 iOS 上加载外部 swf

flash - 检查RTMP广播质量

actionscript-3 - 如何设置拖动X限制

java - 我可以在不向监听器抛出事件的情况下为 JList 设置所选项目吗?