8 Replies Latest reply on Aug 3, 2007 11:38 AM by Rothrock

    Help with Event listeners

    _pixel_chick
      Flash CS3, actionscript 2.0

      I have a timeline animation that I am trying to convert to actionscript. Basically there are 8 movie clips that I have added to the timeline that fade in at different times. I have the code for each of the tweens figured out (var laugh:Tween = new Tween(laugh_mc, "_alpha", Strong.easeIn, 0, 100, 17, false) but I need one to start one, then when its finished, go to the next one without using go to and play. I'm a bit of a newbie to actionscript so if someone could provide an example, it would be much appreciated.

      Thanks!
        • 1. Re: Help with Event listeners
          Level 7
          Hi Pixel Chick,

          I haven't fully tested this but I believe you will want something like
          this:

          laugh.addEventListener(TweenEvent.MOTION_FINISH ,this.onLaughDone);

          ...define a function named "onLaughDone" that will receive a
          "TweenEvent" type object as a parameter. You'll also need to add
          "import fl.transitions.TweenEvent;" to your class header.

          Each new tween object you create afterwards will need its own listener,
          bound in the same way. For example, if you created a variable
          named "laugh2:Tween = new Tween...":

          laugh2.addEventListener(TweenEvent.MOTION_FINISH ,this.onLaughDone);

          ...this points to the same function but you can point it to a completely
          different one.

          Hope this helps,
          Patrick

          _pixel_chick wrote:
          > Flash CS3, actionscript 2.0
          >
          > I have a timeline animation that I am trying to convert to actionscript.
          > Basically there are 8 movie clips that I have added to the timeline that fade
          > in at different times. I have the code for each of the tweens figured out (var
          > laugh:Tween = new Tween(laugh_mc, "_alpha", Strong.easeIn, 0, 100, 17, false)
          > but I need one to start one, then when its finished, go to the next one without
          > using go to and play. I'm a bit of a newbie to actionscript so if someone could
          > provide an example, it would be much appreciated.
          >
          > Thanks!
          >

          --
          http://www.baynewmedia.com
          Faster, easier, better...ActionScript development taken to new heights.
          Download the BNMAPI today. You'll wonder how you ever did without it!
          Available for ActionScript 2.0/3.0.
          • 2. Re: Help with Event listeners
            Rothrock Level 5
            Almost, but that syntax is for AS3. For AS2 you will want:

            laugh.onMotionFinished=laughFinish;

            function laughFinish(){
            //start the next one, do whatever else needs to happen
            }
            • 3. Re: Help with Event listeners
              _pixel_chick Level 1
              Thank you, that seems to be working for part of it. Although, it plays the first 2 clips then goes back and plays the first one again without going through the rest of them. This is what it looks like. It only runs through laugh and tighten. Any thoughts?


              import mx.transitions.Tween;
              import mx.transitions.easing.*;

              var laugh:Tween = new Tween(laugh_mc, "_alpha", Strong.easeIn, 0, 100, 2, true);


              laugh.onMotionFinished = function(){
              var tighten:Tween = new Tween(tighten_mc, "_alpha", Strong.easeIn, 0, 100, 2.0, true);
              }

              tighten.onMotionFinished = function(){
              var lengthen:Tween = new Tween(lengthen_mc, "_alpha", Strong.easeIn, 0, 100, 2, true);
              }

              Thanks!
              • 4. Re: Help with Event listeners
                Rothrock Level 5
                It is some kind of scope issue. For example notice the structure here: tighten.onMotionFinished is at the same level as laugh.onMotionFinished.

                But tighten is defined inside laugh.onMotionFinished, that means there is no tighten tween to attach the onMotionFinished event handler to at the time you are trying to do so. So either you need to define all your tweens at the same level or you need to nest your event handlers inside of each other.

                Or maybe that isn't it at all, but I think it is.
                • 5. Re: Help with Event listeners
                  Level 7
                  Is it not possible to simply create an event listener using the
                  "addEventListener" method? This should prevent scoping issues
                  altogether. Or is this an ActionScript 2.0 thing?

                  Patrick

                  Rothrock wrote:
                  > It is some kind of scope issue. For example notice the structure here:
                  > tighten.onMotionFinished is at the same level as laugh.onMotionFinished.
                  >
                  > But tighten is defined inside laugh.onMotionFinished, that means there is no
                  > tighten tween to attach the onMotionFinished event handler to at the time you
                  > are trying to do so. So either you need to define all your tweens at the same
                  > level or you need to nest your event handlers inside of each other.
                  >
                  > Or maybe that isn't it at all, but I think it is.
                  >
                  • 6. Re: Help with Event listeners
                    Rothrock Level 5
                    You know, I've never tried. Perhaps it would work. But again you will need to have the instance of the tween before you can add a listener for it, I think…
                    • 7. Re: Help with Event listeners
                      Level 7
                      That's correct. It was in the first reply in this thread, I believe:

                      laugh.addEventListener(TweenEvent.MOTION_FINISH ,this.onLaughDone);

                      Not sure if pixel chick tried this but the thread seems to imply not.
                      Well, there it is...

                      Patrick

                      Rothrock wrote:
                      > You know, I've never tried. Perhaps it would work. But again you will need to have the instance of the tween before you can add a listener for it, I think?

                      • 8. Re: Help with Event listeners
                        Rothrock Level 5
                        That notation is for AS3, there is no TweenEvent class in AS2. Nor is there an addEventListener method for the Tween class in AS2. You can use a generic object as a listener as in the code below.

                        But whatever method you use you still have the problem that if laugh doesn't exist at the time you do laugh.addListener(listener); it won't work.

                        Either all the tweens need to be defined at the same time or the onMotionFinished event handlers need to be nested.