6 Replies Latest reply on Mar 12, 2011 8:48 AM by chris2.718

    Problem with eventListeners

    chris2.718

      I have a slideshow in Flash/AS3 with frames having stop() commands, and goforward/goback buttons.

       

      I want to have some action on two of my frames. This is what I'm trying:

       

      myTimer = new Timer(40,0)

      myTimer.start()

      myTimer.addEventListener(TimerEvent.TIMER,update)

       

      where update (and update1) are functions with the scripts I need, one for each frame.

       

      It works fine, but the problem arises once you go to the second frame and then back to the first frame, the action becomes much slower and jagged. I believe this is because both eventListeners are active at the same time. How should I remedy this? Where should I place myTimer.removeEventListener?

        • 1. Re: Problem with eventListeners
          Ned Murphy Adobe Community Professional & MVP

          What is going on in your update function.  I'd say that's where you should remove the listener, but without knowing what the purpose is, it's hard to say.  You should be adding the listener before you start the Timer.

          • 2. Re: Problem with eventListeners
            chris2.718 Level 1

            Why should the removeListener(...) be placed within the update function? Wouldn't that remove the listener and stop the timer events calling the update function? There wouldn't be much action for long.

             

            The content of the update functions is not related to the timer or eventListener: I have commands like   mClip.x = Math.sin(...), mClip2.graphics.beginGradientFill(...), mClip2.graphics.drawRect(...) etc.

            • 3. Re: Problem with eventListeners
              Ned Murphy Adobe Community Professional & MVP

              Sounds like you have all the answers... no need to have someone try to help.

              • 4. Re: Problem with eventListeners
                chris2.718 Level 1

                No buddy, I wasn't trying to be sarcastic. I'm just confused by your suggestion. I did try it out and it didn't work.

                • 5. Re: Problem with eventListeners
                  Ned Murphy Adobe Community Professional & MVP

                  It's a matter of trying to get an idea of how you designed things and what you are trying to do.  Your initial posting and followup info are confusing/vague and give little idea of what you have and what you are trying to do.   If you have to question the questions, it's not likely to get you very far.

                   

                  Try to give a clear explanation of what you have and what is wrong with it.

                  • 6. Re: Problem with eventListeners
                    chris2.718 Level 1

                    I've created a presentation flash. It has three frames and two layers. There are two buttons named forward and back on layer 1. I use the first frame simply as initialization with the following code:

                     

                    forward.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){nextFrame()})

                    back.addEventListener(MouseEvent.CLICK, function(e:MouseEvent){prevFrame()})

                     

                    I have two movieclips, say mClip1 and mClip, placed on frame 2 and frame 3 respectively on layer 2. Each of these has some moving stuff inside them as I mentioned above and I do this using myTimer1 inside mClip 1 and myTimer2 inside mClip2. The problem arises when the user clicks on the forward and back button to shuttle between frame 2 and 3, and new eventListeners and/or timers are being created each time, slowing the action tremendously. I can't place the removeEventListener for mClip1 anywhere in frame2 because that would stop the action. I've tried placing it in frame3 but I get errors: apparently I can't access mClip1 when I'm in frame3. Similarly if I create the timers in frame1, I can't access them in the other frames. For example, I tried on frame 1:

                     

                    timer1 = new Timer(40,0)

                     

                    then inside mClip1 in frame 2:   parent.timer1.addEventListener(...)     and I get AS3 complaining that it doesn't know about timer1 (Error 1010 A term is undefined and has no properties)

                     

                    If I write in frame 1:  timer1.addEventListener(TimerEvent.TIMER, mClip1.update)   then AS3 complains that it doesn't know about mClip1.