9 Replies Latest reply on Sep 12, 2011 3:13 PM by prodigalmaster

    hitTest the same movieclip?

    prodigalmaster Level 2

      _root["zombie"+i].onEnterFrame = function() {

      if(this.hitTest(this)){

      trace("hit!");

      }

      }

       

      This does not work, it traces hit! even if none of the movieclips are touching.

       

      Please help and give theory I dont know how else I can do this.

        • 1. Re: hitTest the same movieclip?
          kglad Adobe Community Professional & MVP

          you're testing if each zombie is hitting itself.  you should expect that to always be true.

           

          you want:

           

           

           

          _root["zombie"+i].onEnterFrame = function() {

          for(var j=i+1;j<zombieTotal;j++){

          if(this.hitTest(_root["zombie"+j])){

          trace("zombies "+i+" and "+j+ " hit!");

          }

          }

          }

           


           

          but even that's pretty wasteful because you're using lots of enterframe loops when you only need one.

          1 person found this helpful
          • 2. Re: hitTest the same movieclip?
            prodigalmaster Level 2

            OK I understand what that should do, but for some reaon it doesnt work, also im not totally sure what the j = j+1 does in the for loop, usually I just leave it j = 0 and let the ;j++ do the adding. Unless maybe it is to distinguish between zombie numbers so there isnt zombie6 hitting zombie6 etc.

             

            anyway, thanks but it seems to not work unless I put it wrong:

             

             

            _root["zombie"+i].onEnterFrame = function() {
            
                      for (var q = q+1; q<zombies; q++) { 
            
            //if q = q+1 it never displays the trace even if they hit.
            //However, if q = 0 then it always shows as the zombies hitting all other zombies.
            
                           if (this.hitTest(_root["zombie"+q])) {
                                trace("zombies "+i+" and "+q+" hit!");
                           }
                      }
            
            }
            

            //I already used j in another for loop.

            • 3. Re: hitTest the same movieclip?
              kglad Adobe Community Professional & MVP

              there is no

               

              j=j+1

               

              that was

               

              j=i+1

               

               


              _root["zombie"+i].onEnterFrame = function() {
              
                        for (var q = i+1; q<zombies; q++) { //and that's "i" (eye) plus one
              
                             if (this.hitTest(_root["zombie"+q])) {
                                  trace("zombies "+i+" and "+q+" hit!");
                             }
                        }
              
              }
              

               

              • 4. Re: hitTest the same movieclip?
                prodigalmaster Level 2

                yeah I already tried that and it didnt work anyway,

                • 5. Re: hitTest the same movieclip?
                  kglad Adobe Community Professional & MVP

                  then you did something wrong or have additional problems because my suggestion is correctly identifies and solves the problem you showed in this thread.

                  • 6. Re: hitTest the same movieclip?
                    prodigalmaster Level 2

                    OK that is fair, I understand the logic of the code and I do not understand why it still doesnt work. Here is this section of code...

                     

                     

                    //variables
                    for (i=0; i<zombies; i++) {
                         _root["zombie"+i] = _root.attachMovie("zombie", "zombie"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
                         _root["zombie"+i]._x = random(Stage.width);
                         _root["zombie"+i]._y = random(Stage.height);
                         _root["zombie"+i].onEnterFrame = function() {
                              //Z_MOVEMENT
                              if (this._x<player._x) {
                                   this._x += Math.random(zombieSpeed);
                              }
                              if (this._x>player._x) {
                                   this._x -= Math.random(zombieSpeed);
                              }
                              if (this._y<player._y) {
                                   this._y += Math.random(zombieSpeed);
                              }
                              if (this._y>player._y) {
                                   this._y -= Math.random(zombieSpeed);
                              }
                              //    
                              if (player.hitTest(this)) {
                                   playerHealth -= 5;
                                   if (player._x<this._x) {
                                        player._x -= 10;
                                   }
                                   if (player._x>=this._x) {
                                        player._x += 10;
                                   }
                                   if (player._y<this._y) {
                                        player._y -= 10;
                                   }
                                   if (player._y>=this._y) {
                                        player._y += 10;
                                   }
                              }
                              //    
                                for (var q = i+1; q<zombies; q++) { //and that's "i" (eye) plus one
                                   if (this.hitTest(_root["zombie"+q])) {
                                        trace("zombies "+i+" and "+q+" hit!");
                                   }
                              }
                                
                              //
                              for (var j = 0; j<1000; j++) {
                                   if (_root["bullet"+j].hitTest(this)) {
                                        removeMovieClip(_root["bullet"+j]);
                                        zombieHealth -= weaponDamage;
                                        if (this._x<player._x) {
                                             this._x -= random(7);
                                        }
                                        if (this._x>=player._x) {
                                             this._x += random(7);
                                        }
                                        if (this._y<player._y-5) {
                                             this._y -= random(7);
                                        }
                                        if (this._y>=player._y) {
                                             this._y += random(7);
                                        }
                                        if (zombieHealth<=0) {
                                             removeMovieClip(this);
                                             zombieHealth = 100;
                                        }
                                   }
                              }
                         };
                    }
                    

                    and everything but the trace works.

                    • 7. Re: hitTest the same movieclip?
                      kglad Adobe Community Professional & MVP

                      that's a mess.

                       

                      try:

                       


                      var zombieA:Array = [];
                      for (var i:Number=0; i<zombies; i++) {
                           var z:MovieClip = _root.attachMovie("zombie", "zombie"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
                           z._x = random(Stage.width);
                           z._y = random(Stage.height);
                      z.xspeed=Math.round(Math.random()*zombieSpeed);
                      z.yspeed=Math.round(Math.random()*zombieSpeed);
                      z.health=100;
                      zombieA.push(z);
                      }

                      var bulletA:Array = [];

                      for (i = 0; i<1000; j++) { bulletA.push(_root["bullet"+i]);
                      }

                      this.onEnterFrame=loopF;  // assuming "this" refers to a movieclip

                      function loopF(){
                      for(var i:Number=zombieA.length-1;i>=0;i--){
                      if(zombieA[i]._x<player_x){
                      zombieA[i]._x+=zombieA[i].xspeed;
                      } else {
                      zombieA[i]._x-=zombieA[i].xspeed;
                      }
                      if(zombieA[i]._y<player_y){
                      zombieA[i]._y+=zombieA[i].yspeed;
                      } else {
                      zombieA[i]._y-=zombieA[i].yspeed;
                      }

                      }
                      }
                      if (player.hitTest(zombieA[i])) {                playerHealth -= 5;                if (player._x<zombieA[i]._x) {                     player._x -= 10;                }                if (player._x>=zombieA[i]._x) {                     player._x += 10;                }                if (player._y<zombieA[i]._y) {                     player._y -= 10;                }                if (player._y>=zombieA[i]._y) {                     player._y += 10;                }           }                for (var q = i-1;q>=0;q--) { //and that's "i" (eye) plus one                if (zombieA[i].hitTest(zombieA[q])) {                     trace("zombies "+i+" and "+q+" hit!");                }           }                        //           for (var j = bulletA.length-1;j>=0; j--) {  //                 if (bulletA[j].hitTest(zombieA[i])) {                     removeMovieClip(bulletA[j]);
                      bulletA.splice(j,1);
                                           zombieA[i].health -= weaponDamage;                                          if (zombieA[i].health<=0) {                          removeMovieClip(zombieA[i]);
                      zombieA.splice(i,1);                                 }                }           }      }; }


                      • 8. Re: hitTest the same movieclip?
                        kglad Adobe Community Professional & MVP

                        this forum total screwed that code formatting.

                         

                        you should manually format that code or cut and paste small snippets into another fla to auto-format small snippets at any one time.

                        • 9. Re: hitTest the same movieclip?
                          prodigalmaster Level 2

                          OK, formatted correctly in flash, I had to get rid of 2 } braces...

                           

                          oh and, ive probably got millions of bad habbits in my coding, I taught myself, without reading large books.

                           

                          You really dont wanna see all my code... there is over 1000 lines of retardedness. Well, atleast most of it works, right??

                           

                          Il get back later after testing that code and seeing what I can do with it.

                          edit: here is it formatted.

                           

                          var zombieA:Array = [];
                          for (var i:Number = 0; i<zombies; i++) {
                               var z:MovieClip = _root.attachMovie("zombie", "zombie"+_root.getNextHighestDepth(), _root.getNextHighestDepth());
                               z._x = random(Stage.width);
                               z._y = random(Stage.height);
                               z.xspeed = Math.round(Math.random()*zombieSpeed);
                               z.yspeed = Math.round(Math.random()*zombieSpeed);
                               z.health = 100;
                               zombieA.push(z);
                          }
                          
                          var bulletA:Array = [];
                          for (i=0; i<1000; j++) {
                               bulletA.push(_root["bullet"+i]);
                          }
                          
                          this.onEnterFrame = loopF;// assuming "this" refers to a movieclip
                          
                          function loopF() {
                               for (var i:Number = zombieA.length-1; i>=0; i--) {
                                    if (zombieA[i]._x<player_x) {
                                         zombieA[i]._x += zombieA[i].xspeed;
                                    } else {
                                         zombieA[i]._x -= zombieA[i].xspeed;
                                    }
                                    if (zombieA[i]._y<player_y) {
                                         zombieA[i]._y += zombieA[i].yspeed;
                                    } else {
                                         zombieA[i]._y -= zombieA[i].yspeed;
                                    }
                          
                          
                               }
                          }
                          if (player.hitTest(zombieA[i])) {
                               playerHealth -= 5;
                               if (player._x<zombieA[i]._x) {
                                    player._x -= 10;
                               }
                               if (player._x>=zombieA[i]._x) {
                                    player._x += 10;
                               }
                               if (player._y<zombieA[i]._y) {
                                    player._y -= 10;
                               }
                               if (player._y>=zombieA[i]._y) {
                                    player._y += 10;
                               }
                          }
                          for (var q = i-1; q>=0; q--) {//and that's "i" (eye) plus one 
                               if (zombieA[i].hitTest(zombieA[q])) {
                                    trace("zombies "+i+" and "+q+" hit!");
                               }
                          }
                          for (var j = bulletA.length-1; j>=0; j--) {
                               if (bulletA[j].hitTest(zombieA[i])) {
                                    removeMovieClip(bulletA[j]);
                                    bulletA.splice(j,1);
                                    zombieA[i].health -= weaponDamage;
                                    if (zombieA[i].health<=0) {
                                         removeMovieClip(zombieA[i]);
                          
                                         zombieA.splice(i,1);
                                    }
                               }
                          }
                          

                           

                          should display in browser correctly...

                           

                          anyway, i just pasted that code in place of the code i showed before. The code made it freeze and abort script. I edited a couple of things and it stopped it freezing but broke other code..

                           

                          I really need to learn proper coding. I have a chuck of code a couple hundred lines lone similar to what I posted, then about 800 lines of weird small repeated functions with different names etc... really need to learn.:/