6 Replies Latest reply on Jul 1, 2009 7:49 AM by kglad

    Help with hitTest

    sendi11

      Hi,

       

      Im new to flash and im trying to make a game. Im stuck on hitTest. I have many black boxes fallling down and i want the game to reset when the black boxes touch my red circle. I used this code for my boxes:

       

      [AS]

      this.createEmptyMovieClip("canvas_mc",10);

       

      myInterval = setInterval(addbox1,500);

       

      function addbox1() {
          var t:MovieClip = canvas_mc.attachMovie("box1","boxy"+i,canvas_mc.getNextHighestDepth());
          i++;
          t._xscale = t._yscale = Math.random()*90+10;
          t._x = Math.random()*Stage.width;
          t._y = -50;
          stop();
          }[/AS]
      My red circle is controlled by the mouse. The code for that is:
      [AS]
      onClipEvent (load) {
      startDrag(this, true);
      }[/AS]
      If someone could tell me how to make the hitTest work i would really apreciate it.

      Thanks
        • 1. Re: Help with hitTest
          kglad Adobe Community Professional & MVP

          use:


          this.createEmptyMovieClip("canvas_mc",10);

           

          var mcA:Array=[]

          myInterval = setInterval(addbox1,500);
          hittestI = setInterval(hittestF,50);

          function hittestF(){
          for(var i=mcA.length-1;i>=0;i--){
          if(redcircle.hitTest(mcA[i])){
          mcA.splice(i,1);
          // +hittest. do whatever
          }
          }
          }


          function

           

          function addbox1() {
              var t:MovieClip = canvas_mc.attachMovie("box1","boxy"+i,canvas_mc.getNextHighestDepth());
             mcA.push(t);
              i++;
              t._xscale = t._yscale = Math.random()*90+10;
              t._x = Math.random()*Stage.width;
              t._y = -50;
              stop();
              }[/AS]
          My red circle is controlled by the mouse. The code for that is:
          [AS]
          onClipEvent (load) {
          startDrag(this, true);
          }[/AS]
          If someone could tell me how to make the hitTest work i would really apreciate it.

          Thanks
          1 person found this helpful
          • 2. Re: Help with hitTest
            sendi11 Level 1

            I just tried that. I cant get it to work. The boxes dont fall and there is an error in line 19. It says function name expected. Im not sure what you have to do.

            • 3. Re: Help with hitTest
              kglad Adobe Community Professional & MVP

              between my code and yours is a straggler: function

               

               

              delete that.

               

               

              for the boxes to fall make your attached box tween from stage top to stage bottom and on its last frame attach:

               

              parent.mcA.splice(indexOf(mcA,this));

              this.removeMovieClip();

               

              function indexOf(a:Array,mc:MovieClip):Number{

              for(var i=0;i<a.length;i++){

              if(a[i]==mc){

              return i;

              }

              }

              }

              • 4. Re: Help with hitTest
                sendi11 Level 1

                That solved the errors i was getting but nothing is happening. The boxes are just falling throught the red circle.

                • 5. Re: Help with hitTest
                  sendi11 Level 1

                  Im not sure what to put in line 12 ("// +hittest. do whatever"). I want it to go back to frame one which is called "start1"

                  • 6. Re: Help with hitTest
                    kglad Adobe Community Professional & MVP

                    i didn't know exactly what you wanted to do when there's a positive hittest.  that's why i inserted the comment about a positive hittest.

                     

                    that's the place where you should place your code that does what you want:  ie, _root.gotoAndPlay("start1") or gotoAndStop()