4 Replies Latest reply on Aug 14, 2010 2:55 PM by prodigalmaster

    hitTest not working

    WFakhrex

      Hey,

      I'm trying to make a hitTest between to movie clips (Let's call them J and T)

       

      These are the actions of J:

       

      onClipEvent (load) {
          xspeed = Math.random()*5;
          yspeed = Math.random()*11;
      }
      onClipEvent (enterFrame) {
          this._x += xspeed;
          this._y += yspeed;
          if (this._x > Stage.width) {
              this._x = 1;
          }
          else if(this._x < 0) {
              this._x = Stage.width-1;
          }
          if (this._y > Stage.height) {
              this._y = 1;
          }

       

      }

       

      And i'm trying to insert the code:

       

      if(this.hitTest(_root.T)){

           gotoAndPlay(2)

      }

       

      But nothing happens when the two movie clips collide.

      What am I doing wrong?

        • 1. Re: hitTest not working
          kglad Adobe Community Professional & MVP

          you really shouldn't attach any code to an object.  but if you did, you would need to add your hitTest to a loop:

           

          WFakhrex wrote:

           

          Hey,

          I'm trying to make a hitTest between to movie clips (Let's call them J and T)

           

          These are the actions of J:

           

          onClipEvent (load) {
              xspeed = Math.random()*5;
              yspeed = Math.random()*11;
          }
          onClipEvent (enterFrame) {
              this._x += xspeed;
              this._y += yspeed;
              if (this._x > Stage.width) {
                  this._x = 1;
              }
              else if(this._x < 0) {
                  this._x = Stage.width-1;
              }
              if (this._y > Stage.height) {
                  this._y = 1;
              }

          if(this.hitTest(_root.T)){

               gotoAndPlay(2);  // which timeline are you trying to direct to frame 2?

          }

           

          }

           


          • 2. Re: hitTest not working
            WFakhrex Level 1

            I don't really understand what you mean.. How do I add a loop?

            • 3. Re: hitTest not working
              kglad Adobe Community Professional & MVP

              the enterFrame function is a loop and i added the code to that loop for you.

               

              just copy the code i suggested and see if it does what you want.

              • 4. Re: hitTest not working
                prodigalmaster Level 2

                A loop is when code is repeated over and over.

                An onEnterFrame loop is when code gets repeated the speed of the fps, so if the fps(frames per second) is 12, the code gets repeated 12 times a second.

                As kglad said you should only add code on the timeline, not inside movieclips or buttons as from experience these usually cause errors and are generally difficult to handle.

                For code on the timeline, when you want to test something you need to use a loop so all you need to do is put all your code inside:

                 

                onEnterFrame = function(){
                     //code here
                };
                

                 

                if the code is FOR a movieclip the add the movieclip instance name (in the property inspector) before onEnterFrame then a dot, like this:

                 

                movieclipName.onEnterFrame = function(){
                     //code here
                };