6 Replies Latest reply: Jul 15, 2010 9:19 PM by T_Townrampage RSS

    hitTestObject causing loop in AS3

    T_Townrampage Community Member

      Im using hitTestObject to start a mc playing, but it continues to loop even though there is a stop(); on its last frame.

       

      Here is my code

       

      var rewind:Boolean = false;
      trig.addEventListener(Event.ENTER_FRAME, enter_frame);
      rolly.addEventListener(Event.ENTER_FRAME, enter_frame);

       

      function enter_frame(event:Event):void{
          if (trig.hitTestObject(slider_mc)) {
              rolly.play();
              rewind = false;
          }else{
              rewind = true;
          }
      }

       


      rolly.myBT.addEventListener(Event.ENTER_FRAME,revFrame);
      function revFrame(e:Event):void{
          if(rewind == true){
              rolly.prevFrame();
          }
      }

       

       

      Any help is much much appreciated.

      The mc "rolly" is the one being looped.  Its animation is within itself (hope that makes sense) it is not looping the main timeline.

        • 1. Re: hitTestObject causing loop in AS3
          Ned Murphy CommunityMVP

          I didn't really focus on trying to understand the code, but if you have ENTER_FRAME listeners assigned, then you might want to remove them once the hitTestObject bit occurs, otherwise they will continue firing.

          • 2. Re: hitTestObject causing loop in AS3
            T_Townrampage Community Member

            Thanks Ned, how would I remove it?

            Im a newbie to AS3, and I come to this forum first to learn form the masters!

            • 3. Re: hitTestObject causing loop in AS3
              T_Townrampage Community Member

              I have tried removing the ENTER_FRAME with no luck, it is still looping.

              Im probably doing it wrong, here is my code

               

              var rewind:Boolean = false;
              trig.addEventListener(Event.ENTER_FRAME, enter_frame);
              rolly.addEventListener(Event.ENTER_FRAME, enter_frame);

               

              function enter_frame(event:Event):void{
                  if (trig.hitTestObject(slider_mc)) {
                      removeEventListener(Event.ENTER_FRAME,enter_frame)
                      rolly.play();
                      rewind = false;
                  }else{
                      rewind = true;
                  }
              }

               


              rolly.myBT.addEventListener(Event.ENTER_FRAME,revFrame);
              function revFrame(e:Event):void{
                  if(rewind == true){
                      rolly.prevFrame();
                  }
              }

               

               

              Any thoughts?

              • 4. Re: hitTestObject causing loop in AS3
                T_Townrampage Community Member

                I got it to work.

                The loop is stopped!!

                But when the  hitTestObject is broken or when the mc's are not touching the mc should  reverse, like on an old school rollOut.

                 

                What am I doing  wrong??

                 

                var rewind:Boolean = false;

                 

                rolly.addEventListener(Event.ENTER_FRAME, enter_frame);

                 

                function enter_frame(event:Event):void{
                    if (trig.hitTestObject(slider_mc)) {
                        rolly.removeEventListener(Event.ENTER_FRAME,enter_frame)
                        rolly.play();
                        rewind = false;
                    }else{
                       
                        rewind = true;
                    }
                }

                 


                rolly.addEventListener(Event.ENTER_FRAME,revFrame);
                function revFrame(e:Event):void{
                    if(rewind == true){
                        rolly.prevFrame();
                    }
                }

                • 5. Re: hitTestObject causing loop in AS3
                  Ned Murphy CommunityMVP

                  I'm still trying to avoid understanding the code  If you're always going to have an ENTER_FRAME active then maybe you can combine them into one.

                   

                  rolly.addEventListener(Event.ENTER_FRAME, enter_frame);

                   

                  function enter_frame(event:Event):void{
                      if (trig.hitTestObject(slider_mc)) {
                          if(rolly.currentFrame < rolly.totalFrames)

                                 rolly.play();

                          }
                      } else {
                          rolly.prevFrame();
                      }
                  }

                  • 6. Re: hitTestObject causing loop in AS3
                    T_Townrampage Community Member

                    You nailed it!

                    Thank you my friend.