【问题标题】:ActionScript - Get Function Of Event DispatcherActionScript - 获取事件调度程序的功能
【发布时间】:2011-01-03 17:21:35
【问题描述】:

我的代码中有 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()");
     }

【问题讨论】:

    标签: actionscript-3 events event-handling get dispatcher


    【解决方案1】:

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

    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);
         }
    

    【讨论】:

    • 哦...抱歉,我的问题写错了。两个补间有相同的名字。我已经编辑了问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-20
    • 1970-01-01
    • 2014-03-05
    • 1970-01-01
    相关资源
    最近更新 更多