开发者

How to use the same function for both a Timer event and Mouse event in AS3?

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;
    开发者_Go百科    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


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
      }
      //...
    }
    


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜