ActionScript - 获取事件调度程序的函数

发布于 2024-10-10 11:06:30 字数 946 浏览 3 评论 0原文

我的代码中有 2 个函数可以调度 TweenEvent。每个函数调度相同的补间并添加相同的 TweenEvent.MOTION_FINISH 事件侦听器。但是,事件侦听函数必须根据调度事​​件的函数进行操作。

是否可以从事件监听器中获取事件调度程序的功能?如果没有其他优雅的解决方案,我可以使用一个标志来完成这项工作。

public function FirstTweenAction():void
     {
     myTween = new Tween(/* tween stuff */);
     myTween.addEventListener(TweenEvent.MOTION_FINISH, myTweenEventMotionFinishHandler);
     }

public function SecondTweenAction():void
     {
     myTween = new Tween(/* tween stuff */);
     myTween.addEventListener(TweenEvent.MOTION_FINISH, myTweenEventMotionFinishHandler);
     }

private function myTweenEventMotionFinishHandler(evt:TweenEvent):void
     {
     evt.currentTarget.removeEventListener(TweenEvent.MOTION_FINISH, myTweenEventMotionFinishHandler);

     if (/* Event was fired from FirstTweenAction() */)
        trace("Dispatcher is FirstTweenAction()");
        else     
        trace("Dispatcher is SecondTweenAction()");
     }

there are 2 functions in my code that dispatch a TweenEvent. each function dispatches the same tween and adds the same TweenEvent.MOTION_FINISH event listener. however, the event listening function must act according to which function dispatched the event.

is it possible to get the function of the event dispatcher from the event listener? i could use a flag to make this work if there are no other elegant solutions.

public function FirstTweenAction():void
     {
     myTween = new Tween(/* tween stuff */);
     myTween.addEventListener(TweenEvent.MOTION_FINISH, myTweenEventMotionFinishHandler);
     }

public function SecondTweenAction():void
     {
     myTween = new Tween(/* tween stuff */);
     myTween.addEventListener(TweenEvent.MOTION_FINISH, myTweenEventMotionFinishHandler);
     }

private function myTweenEventMotionFinishHandler(evt:TweenEvent):void
     {
     evt.currentTarget.removeEventListener(TweenEvent.MOTION_FINISH, myTweenEventMotionFinishHandler);

     if (/* Event was fired from FirstTweenAction() */)
        trace("Dispatcher is FirstTweenAction()");
        else     
        trace("Dispatcher is SecondTweenAction()");
     }

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

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

发布评论

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

评论(1

眼趣 2024-10-17 11:06:30

您无法找出补间是通过哪个方法初始化的。相反,创建两个补间成员变量并检查事件的目标对象:

if (evt.target == myFirstTween) doSomething();
else doSomethingElse();

或调用两个不同的事件处理程序:

public function FirstTweenAction():void
     {
     myTween = new Tween(/* tween stuff */);
     myTween.addEventListener(TweenEvent.MOTION_FINISH, myFirstTweenEventMotionFinishHandler);
     }

public function SecondTweenAction():void
     {
     myTween = new Tween(/* tween stuff */);
     myTween.addEventListener(TweenEvent.MOTION_FINISH, mySecondTweenEventMotionFinishHandler);
     }

You can't find out from which method the tween was initialized. Instead, make two tween member variables and check the event's target object:

if (evt.target == myFirstTween) doSomething();
else doSomethingElse();

or call two different event handlers:

public function FirstTweenAction():void
     {
     myTween = new Tween(/* tween stuff */);
     myTween.addEventListener(TweenEvent.MOTION_FINISH, myFirstTweenEventMotionFinishHandler);
     }

public function SecondTweenAction():void
     {
     myTween = new Tween(/* tween stuff */);
     myTween.addEventListener(TweenEvent.MOTION_FINISH, mySecondTweenEventMotionFinishHandler);
     }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文