如何通过 addEventListener 传递语句?

发布于 2025-01-02 11:52:12 字数 196 浏览 1 评论 0原文

我一直在尝试通过动作脚本中的 addEventListener 事件传递参数,例如...

target.addEventListener("pComp", rakeSoil(target));

但我收到错误。

我尝试用谷歌搜索,但没有成功:/

如果你这样做的话,谢谢你的回复:)

Iv been trying to pass arguments through an addEventListener event in actionscript such as...

target.addEventListener("pComp", rakeSoil(target));

but i get errors.

Iv tried to google but no luck :/

Thanks for replying if you do :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

下壹個目標 2025-01-09 11:52:12

目标已作为事件的一部分传递,event.currentTargetevent.target 将是您想要的。

如果您想要传递其他内容,请创建自定义事件。将属性添加到自定义事件。

The target is already passed as part of the event, either event.currentTarget or event.target will be what you want.

If you want something else passed, create a custom event. Add the property to the custom event.

何处潇湘 2025-01-09 11:52:12

尝试添加一个附加方法作为事件侦听器:

target.addEventListener ("pComp", targetListener);

...

private function targetListener (event:Event):void {

   rakeSoil (event.currentTarget);

}

Try adding an additional method as your event listener:

target.addEventListener ("pComp", targetListener);

...

private function targetListener (event:Event):void {

   rakeSoil (event.currentTarget);

}
撩发小公举 2025-01-09 11:52:12

这就是你想要的:

   {
        var target:EventDispatcher = ...;
        Function rakeSoil = function (e:Event):void 
        {
            // handle target 
        }

        target.addEventListener("pComp", rakeSoil);
    }

rakeSoil 是一个一流的函数(或闭包),当事件被调度时,它将被调用,并且你可以访问其中的“目标”。

编辑:

看看 Closure(计算机科学)

How this is what you want:

   {
        var target:EventDispatcher = ...;
        Function rakeSoil = function (e:Event):void 
        {
            // handle target 
        }

        target.addEventListener("pComp", rakeSoil);
    }

rakeSoil is a first class function(or closure), when event is dispatched, it will be invoked, and you can access 'target' in it.

EDIT:

Have a look at Closure (computer science)

无戏配角 2025-01-09 11:52:12

我一直发现匿名函数带来的麻烦比其价值更大。我将简单地遵循标准事件处理程序代码布局。它更正式,需要更多的努力,但是没有歧义,并且当您一年后返回它时,它的可读性要高得多(减少了令人头疼的持续时间):

//  Target extends EventDispatcher
private var target:Target;

public function listenToTarget();
{
    target = new Target();
    target.addEventListener("pComp", pCompHandler);
}

private function pCompHandler(event:Event):void
{
    target.rakeSoil();
}

虽然,现在我更仔细地观察它,为什么让这个对象做一些 Target 应该能够在内部自行处理的事情?

I have always found anonymous functions to be more trouble than they are worth. I would simply follow the standard event handler code layout. It's more formal and takes a little more effort up front, but there is no ambiguity and it is far more readable when you return to it a year from now (reduces head-scratching duration):

//  Target extends EventDispatcher
private var target:Target;

public function listenToTarget();
{
    target = new Target();
    target.addEventListener("pComp", pCompHandler);
}

private function pCompHandler(event:Event):void
{
    target.rakeSoil();
}

Although, now that I look at it more closely, why are you having this object do something that Target should be capable of handling internally on its own?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文