4 Replies Latest reply on Jul 25, 2009 3:33 PM by dalcde

    super complicated hitTest bug

    dalcde Level 1

      I was making a game, which you shoot fireballs from the sky and attack enemies. The fireball will explode when it touches the ground. This is the code of the explosion, which is a movie of 20 frames. I made a proccessor, named proccessor2, ( there is a proccessor1, of courese ). There is also a MC which stores variables, such as the attack1damage. the problem is, if it only hits one enemy, it should just attack once, with the first type. It would then trace 1. but if I shoot two fireballs together, it starts to attack more than once from the next. At the first time, it traces 1 2, then on the second time, it traces 1 2 2, then 1 2 2 2, etc. What's the problem? This is the code:

       

          var attack;
          var enemiesKilled;
          var enemyKill;
          function kill() {
              enemyKill = 0;
              for (var i in _root.proccessor2.enemies) {
                  if (this.enemiesKilled.length == 0) {
                      if (this.hitTest(_root.proccessor2.enemies[i])) {
                          _root.proccessor2.enemies[i].health -= attack;
                          this.enemiesKilled.push(i);

                          trace(1)
                      }
                  } else {
                      for (var j in enemiesKilled) {
                          if (enemiesKilled[j] == i) {
                              enemyKill++;
                          }
                      }
                      trace(enemyKill);
                      if (this.hitTest(_root.proccessor2.enemies[i]) && enemyKill == 0) {
                          _root.proccessor2.enemies[i].health -= attack;
                          this.enemiesKilled.push(i);

                          trace(2)
                      }
                  }
              }
          }
          function onLoad() {
              enemyKill = 0;
              attack = _root.storage.attack1damage;
              enemiesKilled = new Array();
          }
          function onEnterFrame() {
              if (this._currentframe<20) {
                  kill();
              }
              if (this._currentframe == this._totalframes) {
                  this.removeMovieClip();
              }
          }

       

       

      I know this may be a terrible way to do it, and you can do it in 10 lines of codes, but can you help? I am willing to explain the code if you don't understand, or if you want to know more. This is just a part of the whole game, so I don't want to upload the whole file. Sorry for the unconvenience.

        • 1. Re: super complicated hitTest bug
          dalcde Level 1

          Actually I can combine two "if" codes. But I'm just to lazy to!

          • 2. Re: super complicated hitTest bug
            dalcde Level 1

            Can anybody help?

            • 3. Re: super complicated hitTest bug
              Rothrock Level 5

              I've looked at this and read it twice and have no idea what the problem is.

               

              You ask, "What's the problem?" and then don't say what the problem is. And this is really less than clear, "but if I shoot two fireballs together, it starts to attack more than once from the next."

               

              And what does this mean, "I made a proccessor, named proccessor2, ( there is a proccessor1, of courese)"?

               

              Generally the way to get help on unanswered questions is to post back the work you have done in the interim that has helped you make some progress in better understanding the situation or to offer up something else you thought of.

               

              But if you are getting multiple traces then that means your must have more things in _root.proccessor2.enemies than you thought. Trace that out just inside your loop:

               

              trace("inspecting proccessor2.enemies "+_root.proccessor2.enemies[i]);

               

              And see what that says.

               

              And if you are troubled by the repeating of 2 then that means that your second hit test is coming back true each time.

              1 person found this helpful
              • 4. Re: super complicated hitTest bug
                dalcde Level 1

                proccessor is something like the one you put in the timeline ( I know it isn't a good way )