2 Replies Latest reply on Jul 24, 2009 10:59 AM by Rothrock

    Making enemies collide with eachother

    Gooms9 Level 1
      Hi, I've got a function here that spawns an enemy for me (thanks kglad) and I'm calling it in a loop every few seconds or so so that it creates a new enemy every second. This all works great. What I want to do though, is to make the enemies collide with each other. How would I go about doing this? I've tried using hitTest but it detects that each enemy is hitting itself which is always. I've attached my code.
        • 1. Re: Making enemies collide with each other
          dalcde Level 1

          I don't see the code, but I have a way to solve the problem, as I needed one.

           

          for (var i in yourArray) {
               if (this.hitTest(yourArray[i]) && yourArray[i]._name != this._name) {
                    trace("hit");
               }

          }

          • 2. Re: Making enemies collide with each other
            Rothrock Level 5

            BTW, this algorithm looks a little naive. Imagine if you have two things you want to check.

             

            And you check if 0 is hitting 1, right? But then do you need to check if 1 is hitting 0? Because if 0 is hitting 1 then 1 is hitting 0 and if not then neither. Right

             

            Now imagine that there are three.

             

            First you check 0-1 and 0-2.

            Next you check 1-2.

             

            but you don't need to check 1-0 or 2-0 or 2-1.

             

            So if you are just checking one thing against a list of things then the algorithm you've shown is fine. But if you need to check several things against several things you would most likely be doing twice as many checks as you need to do.