如何在 AS3 中对计时器事件和鼠标事件使用相同的函数?

发布于 2024-09-10 05:50:10 字数 2727 浏览 2 评论 0原文

所以今天我的问题是我们如何才能让 1 个函数同时由计时器事件和鼠标事件触发?我试图避免仅仅因为我还有一个触发它的鼠标事件而不仅仅是一个计时器事件而必须重新编写/重新命名一个函数。

问题:

我有一个 addThis 气泡淡入,并在按钮翻转时带有图标,addThis 气泡将在计时器完成后淡出,但该气泡中还有一个关闭按钮,需要使用计时器使用的相同功能。 (想要避免编写两个完全相同的函数)

我希望有一个简单的解决方法,例如 event:null 或其他东西。

计时器事件:

private function overShareButton(event:MouseEvent):void
    {
        shareIconsTimer = new Timer(3000,1);
        shareIconsTimer.addEventListener(TimerEvent.TIMER, fadeOutShareIcons);
        shareIconsTimer.start();

        fadeInShareIcons();
    }

关闭按钮事件:

shareIcons.btn_closeIcons.addEventListener(MouseEvent.MOUSE_UP, fadeOutShareIcons);

addThis Bubble 淡出函数:

// Fade out the addThis Icons
    private function fadeOutShareIcons(e:TimerEvent):void
    {
        shareIcons.buttonMode = false;

        shareIcons.btn_closeIcons.buttonMode  = false;
        shareIcons.btn_email.buttonMode       = false;
        shareIcons.btn_facebook.buttonMode    = false;
        shareIcons.btn_myspace.buttonMode     = false;
        shareIcons.btn_digg.buttonMode        = false;
        shareIcons.btn_delicious.buttonMode   = false;
        shareIcons.btn_favorites.buttonMode   = false;
        shareIcons.btn_twitter.buttonMode     = false;
        shareIcons.btn_google.buttonMode      = false;
        shareIcons.btn_messenger.buttonMode   = false;
        shareIcons.btn_stumbleupon.buttonMode = false;

        // STOP TIMER & Remove share icon button events...
        shareIconsTimer.stop();
        shareIconsTimer.removeEventListener(TimerEvent.TIMER, fadeOutShareIcons);
        shareIcons.btn_email.removeEventListener       (MouseEvent.MOUSE_UP, addThisToEmail);
        shareIcons.btn_facebook.removeEventListener    (MouseEvent.MOUSE_UP, addThisToFacebook);
        shareIcons.btn_myspace.removeEventListener     (MouseEvent.MOUSE_UP, addThisToMyspace);
        shareIcons.btn_digg.removeEventListener        (MouseEvent.MOUSE_UP, addThisToDigg);
        shareIcons.btn_delicious.removeEventListener   (MouseEvent.MOUSE_UP, addThisToDelicious);
        shareIcons.btn_favorites.removeEventListener   (MouseEvent.MOUSE_UP, addThisToFavorites);
        shareIcons.btn_twitter.removeEventListener     (MouseEvent.MOUSE_UP, addThisToTwitter);
        shareIcons.btn_google.removeEventListener      (MouseEvent.MOUSE_UP, addThisToGoogle);
        shareIcons.btn_messenger.removeEventListener   (MouseEvent.MOUSE_UP, addThisToLive);
        shareIcons.btn_stumbleupon.removeEventListener (MouseEvent.MOUSE_UP, addThisToStumbleupon);

        TweenLite.to(shareIcons, .2, {alpha:0});
    }

我现在唯一的选择是创建另一个函数来处理关闭按钮 MouseEvent,任何提示/想法表示赞赏! :D

so today my question is how can we have 1 function triggered by both a Timer event and a Mouse event? 'm trying to avoid having to re-write/re-name a function just because I also have a Mouse event that triggers it and not just a Timer event.

Issue:

I have a addThis bubble fade in with icons on a button rollover, the addThis bubble will fade out after the timer is finished, but I also have a close button in that bubble which needs to use the same function that the timer uses. (want to avoid writing 2 exactly alike functions)

I'm hoping this there is an easy workaround for this like event:null or something.

The Timer Event:

private function overShareButton(event:MouseEvent):void
    {
        shareIconsTimer = new Timer(3000,1);
        shareIconsTimer.addEventListener(TimerEvent.TIMER, fadeOutShareIcons);
        shareIconsTimer.start();

        fadeInShareIcons();
    }

The close button event:

shareIcons.btn_closeIcons.addEventListener(MouseEvent.MOUSE_UP, fadeOutShareIcons);

The addThis Bubble Fade out function:

// Fade out the addThis Icons
    private function fadeOutShareIcons(e:TimerEvent):void
    {
        shareIcons.buttonMode = false;

        shareIcons.btn_closeIcons.buttonMode  = false;
        shareIcons.btn_email.buttonMode       = false;
        shareIcons.btn_facebook.buttonMode    = false;
        shareIcons.btn_myspace.buttonMode     = false;
        shareIcons.btn_digg.buttonMode        = false;
        shareIcons.btn_delicious.buttonMode   = false;
        shareIcons.btn_favorites.buttonMode   = false;
        shareIcons.btn_twitter.buttonMode     = false;
        shareIcons.btn_google.buttonMode      = false;
        shareIcons.btn_messenger.buttonMode   = false;
        shareIcons.btn_stumbleupon.buttonMode = false;

        // STOP TIMER & Remove share icon button events...
        shareIconsTimer.stop();
        shareIconsTimer.removeEventListener(TimerEvent.TIMER, fadeOutShareIcons);
        shareIcons.btn_email.removeEventListener       (MouseEvent.MOUSE_UP, addThisToEmail);
        shareIcons.btn_facebook.removeEventListener    (MouseEvent.MOUSE_UP, addThisToFacebook);
        shareIcons.btn_myspace.removeEventListener     (MouseEvent.MOUSE_UP, addThisToMyspace);
        shareIcons.btn_digg.removeEventListener        (MouseEvent.MOUSE_UP, addThisToDigg);
        shareIcons.btn_delicious.removeEventListener   (MouseEvent.MOUSE_UP, addThisToDelicious);
        shareIcons.btn_favorites.removeEventListener   (MouseEvent.MOUSE_UP, addThisToFavorites);
        shareIcons.btn_twitter.removeEventListener     (MouseEvent.MOUSE_UP, addThisToTwitter);
        shareIcons.btn_google.removeEventListener      (MouseEvent.MOUSE_UP, addThisToGoogle);
        shareIcons.btn_messenger.removeEventListener   (MouseEvent.MOUSE_UP, addThisToLive);
        shareIcons.btn_stumbleupon.removeEventListener (MouseEvent.MOUSE_UP, addThisToStumbleupon);

        TweenLite.to(shareIcons, .2, {alpha:0});
    }

My only choice right now is to create another function to handle the close button MouseEvent, any tips/thoughts appreciated! :D

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

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

发布评论

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

评论(2

我们的影子 2024-09-17 05:50:10

这里的问题是您已将 fadeOutShareIcons 参数输入到 TimerEvent,因此它只能接受 TimerEvent

由于 MouseEventTimerEvent 源自 Event,因此您可以使用它,以便两者可以一起工作。如果您还想在不带参数的情况下调用相同的函数,您可以传递设置为 null 的默认值。

  • 案例1

    私有函数 fadeOutShareIcons(e:Event):void
    
  • 案例 2 使用默认参数,以便您可以调用 fadeOutShareIcons()

    私有函数 fadeOutShareIcons(e:Event=null):void
    
  • 您还可以检查事件的来源,检查事件的类型:

    私有函数 fadeOutShareIcons(e:Event=null):void {
      如果(e==null){
        // 直接调用
      } else if (e.type==MouseEvent.MOUSE_UP) {
          // 来自鼠标事件
      } else if (e.type==TimerEvent.TIMER) {
          // 来自定时器事件
      }
      //...
    }
    

Your problem here is you have typed your fadeOutShareIcons argument to a TimerEvent, so it can only accept TimerEvent.

Since MouseEvent and TimerEvent descend from Event you can use it so both can work together. If you want also call the same function without argument you can pass a default value setted to null.

  • case 1

    private function fadeOutShareIcons(e:Event):void
    
  • case 2 with default argument so you can call fadeOutShareIcons()

    private function fadeOutShareIcons(e:Event=null):void
    
  • You can also check from where event you came checking the type of the event:

    private function fadeOutShareIcons(e:Event=null):void {
      if (e==null) {
        // call directly
      } else if (e.type==MouseEvent.MOUSE_UP) {
          // came from mouse event
      } else if (e.type==TimerEvent.TIMER) {
          // came from timer event
      }
      //...
    }
    
渔村楼浪 2024-09-17 05:50:10

将完成工作的代码放在一个单独的函数中(由两个事件处理程序调用)是否有效?无论如何,您的主要工作函数似乎都没有使用事件参数,因此该工作是否在另一个函数中完成也没关系。

Would putting the code that does the work in a separate function, which is called by both event handlers work? Your main work function doesn't appear to use the event parameter anyway, so it shouldn't matter if that work is done in another function.

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