8 Replies Latest reply on May 30, 2007 12:56 PM by kglad

    CS3 Event Handlers

    padrepio Level 1
      I have a MovieClip that I am using as a button (which was previously possible in Flash 8). I created the up, over, and down frames in the button and am writing the AS3 code for the button. It looks like this:

      import flash.events.MouseEvent;

      function playMovie(event:MouseEvent):void
      {
      clip1.play();
      }
      function overState(event:MouseEvent):void {
      playButton.gotoAndStop(5);
      }
      function downState(event:MouseEvent):void {
      playButton.gotoAndStop(10);
      }
      function outState(event:MouseEvent):void {
      playButton.gotoAndStop(1);
      }

      playButton.addEventListener(MouseEvent.CLICK, playMovie);
      playButton.addEventListener(MouseEvent.MOUSE_OVER, overState);
      playButton.addEventListener(MouseEvent.MOUSE_DOWN, downState);
      playButton.addEventListener(MouseEvent.MOUSE_OUT, outState);

      That's an awful lot of code for 1 button. If I had 5 or 6 buttons, that's a lot to edit. Is there an easier way to do this?
        • 1. Re: CS3 Event Handlers
          kglad Adobe Community Professional & MVP
          you can't do it much easier for 1 button. but coding for 100 buttons doesn't require much more coding.

          just use a for-loop to loop assign your addEventListeners and use event.currentTarget in your handler functions instead of playButton. if your buttons don't have conveniently chosen references the references will have to be added to an array so you can use that in your for-loop.
          • 2. Re: CS3 Event Handlers
            Level 7
            padrepio,

            > I have a MovieClip that I am using as a button (which was
            > previously possible in Flash 8).

            It's still possible in Flash CS3, whether you're using ActionScript 2.0
            or 3.0. Depending on the complexity of your movie, you might just want to
            set the Publish Settings for AS2. Maybe?

            > I created the up, over, and down frames in the button and
            > am writing the AS3 code for the button. It looks like this:
            >
            > import flash.events.MouseEvent;

            If you're writing this code in a timeline keyframe, you can omit this
            import statement. That helps a bit. ;)

            > function playMovie(event:MouseEvent):void
            > {
            > clip1.play();
            > }
            > function overState(event:MouseEvent):void {
            > playButton.gotoAndStop(5);
            > }
            > // etc.

            > playButton.addEventListener(MouseEvent.CLICK, playMovie);
            > playButton.addEventListener(MouseEvent.MOUSE_OVER, overState);
            > // etc.

            > That's an awful lot of code for 1 button.

            Well, yes and no. I remember when AS2 was brand new, and instead of
            something like this:

            on (rollOver) {
            // do something
            }

            ... it suddenly became this ...

            btnInstanceName.onRollOver = function():Void {
            // do something
            }

            ... which did look like a whole lot more, I remember. To make the
            formatting analogous, that could have also been done with a named function
            ...

            function customFunction():Void {
            // do something
            }
            btnInstanceName.onRollOver = customFunction;

            ... which is even longer -- but that does enter the ballpark of the
            named-function approach you're using in AS3. Looking at it in that way, the
            AS3 version isn't excessively more ... just a tad more.

            > If I had 5 or 6 buttons, that's a lot to edit. Is there an easier way to
            > do this?

            Well, you could use a for() loop and cycle among your buttons.

            var btns:Array = new Array(btn1, btn2, btn3);

            for (var i:Number = 0; i < btns.length; i++) {
            btns .addEventListener(MouseEvent.CLICK, playMovie);
            btns
            .addEventListener(MouseEvent.MOUSE_OVER, overState);
            btns .addEventListener(MouseEvent.MOUSE_DOWN, downState);
            btns
            .addEventListener(MouseEvent.MOUSE_OUT, outState);
            }

            Of course, your handler functions when then have to check which button
            had been clicked in order to know what to do, but that can be accomplished
            by checking the incoming Event object those functions receive as a
            parameter.

            function playMovie(e:Event):void {
            trace(e.target);
            trace(e.target.name);
            }


            David Stiller
            Adobe Community Expert
            Dev blog, http://www.quip.net/blog/
            "Luck is the residue of good design."


            • 3. Re: CS3 Event Handlers
              Level 7
              Arrg, foiled by the superquick reply of kglad! hehe


              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Re: CS3 Event Handlers
                padrepio Level 1
                Would I need a for loop for every button? Where does event.currentTarget go?
                • 5. Re: CS3 Event Handlers
                  kglad Adobe Community Professional & MVP
                  :

                  • 6. Re: CS3 Event Handlers
                    padrepio Level 1
                    Cool. So the mouse events become general and the for loop is specifically for the buttons themselves? That's much better though than having to create the mouse events for every button.

                    Awesome.
                    • 7. Re: CS3 Event Handlers
                      padrepio Level 1
                      I'm getting the following error with that code:

                      TypeError: Error #1010: A term is undefined and has no properties.
                      at test_button__fla::MainTimeline/test_button__fla::frame1()

                      Checking the syntax doesn't produce an error, but when I test the movie it does. What does this mean?
                      • 8. Re: CS3 Event Handlers
                        kglad Adobe Community Professional & MVP
                        the mouse handlers are mutlipurpose (or general) with each (except playMovie) being the same for all the buttons.

                        you still need a different MOUSE_DOWN handler for each button or a switch of some kind in one handler.

                        but that's no different than as 1 and as 2.

                        and now that i look at it, it's actually less typing to code in as3 than the typical coding used in as2 for multiple buttons in a for-loop.