4 Replies Latest reply on Aug 8, 2011 2:49 AM by LukeElChul

    HitTests Dont Work HELP????

    LukeElChul Level 1

      Ive got this script. when u press a button u can create a movie clip called man. when u press another button u can create one called enemy. thay both create at different sides of the seane and thay walk past each other.

       

      but when thay touch each other thay should go to frames inside them. ect Attack,Die. but thay dont help....

       

       

       

      var mans = 0;

      function addman() {

          if (Gold>=10) {

             var man= attachMovie("man", "man"+mans, _level0.getNextHighestDepth(), {_x:95.1, _y:479});

                 man.onEnterFrame = moveman;

              mans += 1;

            Gold -= 10;

           }

      }

       

      btnMan.onRelease = addman; function moveman() {

           if (this.hitTest(enemy)) { 

             this.gotoAndStop(2);//mans fighting frame

      if(enemy.hitTest(MDS));

      enemy.gotoAndStop(2);// enemys died frame

           } else {

      this.gotoAndStop(1);  //mans walking frame     

      this._x += 1.3;

           }

      }

       

      var enemys = 0;

      function addenemy() {

          if (Gold>=10) {

            var enemy= attachMovie("enemy", "enemy"+enemys, _level0.getNextHighestDepth(), {_x:595.1, _y:479});

                  enemy.onEnterFrame = moveenemy;

              enemys += 1;

            Gold -= 10;

          }

      }

       

      btnEnemy.onRelease = addenemy;

      function moveenemy() {

           if (this.hitTest(man)) { 

             this.gotoAndStop(3);//enemys fighting frame

      if(man.hitTest(EMDS));// a little square called EMDS inside fighting on the frame where he punches

      man.gotoAndStop(3);//mans died frame

           } else {

      this.gotoAndStop(1);  //enemys walking frame

             this._x -= 1.3; 

         }

      }

       

      help

        • 1. Re: HitTests Dont Work HELP????
          Ned Murphy Adobe Community Professional & MVP

          You are still missing things that I told you you needed to include yesterday, mainly the for loop.  The code below should take care of the hittests, I have added the loops.  The only issues I see are...

           

          1. You shouldn't need to do hit tests on both the enemy vs man and man vs enemy.

          2. It is not clear what your intentions are for the MDS and EMDS lines, so I didn't touch them... they are incomplete due to the ";"

           

          var men:Array = new Array();
          var enemies:Array = new Array();

           

          var mans = 0;
          var enemys = 0;

           

          function addman() {
              if (Gold>=10) {
                 men.push(attachMovie("man", "man"+mans, _level0.getNextHighestDepth(), {_x:95.1, _y:479}));
                 men[mans].onEnterFrame = moveman;
                 mans += 1;
                 Gold -= 10;
              }
          }

           

          btnMan.onRelease = addman;

           

          function moveman() {
          for(i=0; i<enemys; i++){
                 if (this.hitTest(enemies[i])) { 
                    this.gotoAndStop(2);//mans fighting frame
              if(enemies[i].hitTest(MDS));
              enemies[i].gotoAndStop(2);// enemys died frame
                 } else {
               this.gotoAndStop(1);  //mans walking frame     
                     this._x += 1.3;
                 }
          }
          }

           

          function addenemy() {
              if (Gold>=10) {
                 enemies.push(attachMovie("enemy", "enemy"+enemys, _level0.getNextHighestDepth(), {_x:595.1, _y:479}));
                 enemies[enemys].onEnterFrame = moveenemy;
                 enemys += 1;
                 Gold -= 10;
              }
          }

           

          btnEnemy.onRelease = addenemy;

           

          function moveenemy() {
              for(i=0; i<mans; i++){
            if (this.hitTest(men[i])) { 
                      this.gotoAndStop(3);//enemys fighting frame
                      if(man.hitTest(EMDS));// a little square called EMDS inside fighting on the frame where he punches
                      man.gotoAndStop(3);//mans died frame
                  } else {
                      this.gotoAndStop(1);  //enemys walking frame
                      this._x -= 1.3; 
               }
          }
          }

           

          You should learn about using the trace command to help you troubleshoot your code.  If you had traced "man" in your moveenemy function you would have found that it is undefined, mainly due to it being declared within the scope of a function where nothing outside the function can see it.  The same applies for the "enemy" inside the moveman function.

          1 person found this helpful
          • 2. Re: HitTests Dont Work HELP????
            LukeElChul Level 1

            That works but only with 2. ive tuck the MDS and the EMDS lines out.

             

            the only problam is that when i crete another man are enemy the original ones start walking again then nothing seems to work and every thing starts walking faster.

             

            Thank You for ur help so far.

            • 3. Re: HitTests Dont Work HELP????
              Ned Murphy Adobe Community Professional & MVP

              There are numerous refinements that you will need to make to that code to get it where you probably want it to be, but I am not going to be doing it for you.  You wanted to get the hitTest working and I explained why what you did wouldn't work, along showing some of the other things I said you need to use that you wouldn't add in yesterday.

               

              As I already mentioned, you should learn how to use the trace() function to help you troubleshoot your code.  It helps you to see if things ar what you think they should be for stuff to work properly.

              1 person found this helpful
              • 4. Re: HitTests Dont Work HELP????
                LukeElChul Level 1

                Thank you for your help I will start using the trace() and hopefully won't ask this forum so much Questions.

                 

                Thank you!!!

                 

                Sent from my iPod