10 Replies Latest reply on Jan 30, 2012 5:24 PM by Ned Murphy

    Collision detection, multiple movieclips, self collision.

    prodigalmaster Level 2

      Here is my code to attach several movieclips onto the stage

      var humans:Number = 90;
      for (i=0; i<humans; i++) {
                mcHuman = attachMovie("mcHuman", "mcHuman"+i, i);
      //blah blah
      }
      

       

      No matter what, it seems impossible to detect collision between mcHuman and mcHuman...

       

      Anyone have any ideas?

      Thanks.

        • 1. Re: Collision detection, multiple movieclips, self collision.
          Ned Murphy Adobe Community Professional & MVP

          What have you tried that is not working?  You have shown nothing to indicate the conditions leading up to collisions.

          • 2. Re: Collision detection, multiple movieclips, self collision.
            prodigalmaster Level 2

            for{...

            mcHuman.onEnterFrame = function() {

                                if (this.hitTest(this)) {

                                                    trace("hit!"); //always traces hit!

                                          }

            };

            }

             

            //didnt work so I try:

             

            if (_root["mcHuman"+i].hitTest(_root["mcHuman"+i-1])) {

                                          trace("hit!");

            }

            //but doesnt trace anything

            //so I try

             

            for (i=0; i<humans; i++) {

            for (j=0; j<humans; j++) {

            onEnterFrame{

                                if (_root["mcHuman"+i].hitTest(_root["mcHuman"+j])) {

                                          trace("hit!");

                                }    

                      };

            }

            }

            //but nope.

            //No freaking idea

            • 3. Re: Collision detection, multiple movieclips, self collision.
              Ned Murphy Adobe Community Professional & MVP

              Try...

               

              this.onEnterFrame = function {

                  for (i=0; i<humans; i++) {

                      for (j=0; j<humans; j++) {

                            if (i != j && _root["mcHuman"+i].hitTest(_root["mcHuman"+j])) {

                                  trace("hit!");

                            }    

                      }

                  }

              }

              1 person found this helpful
              • 4. Re: Collision detection, multiple movieclips, self collision.
                prodigalmaster Level 2

                //hmm well code is needed outside of the enterframe loop too.

                for (i=0; i<humans; i++) {

                          for (j=0; j<humans; j++) {

                                    mcHuman = attachMovie("mcHuman", "mcHuman"+i, i);

                                    mcHuman._x = random(Stage.width);

                                    mcHuman._y = random(Stage.height);

                                    mcHuman.xTarget = random(20)-10;

                                    mcHuman.yTarget = random(20)-10;

                          }

                }

                _root.onEnterFrame = function() {

                          for (i=0; i<humans; i++) {

                                    for (j=0; j<humans; j++) {

                                              if (_root["mcHuman"+i].hitTest(_root["mcHuman"+j])) {

                                                        trace("hit!");

                                              }

                               ///

                          }             

                                    }

                          }

                 

                 

                };

                //this doesnt work it freaks out

                • 5. Re: Collision detection, multiple movieclips, self collision.
                  Ned Murphy Adobe Community Professional & MVP

                  There is likely more code than you realize that is needed, but that's up to you to decide on.

                   

                  Your first set of loops make no sense, just one loop is needed like you had originally.  That could be one reason for things going crazy.

                   

                  As far as the enterframe function goes, you have an excess of curly braces in it, which could also be a reason for things going crazy.  Also, if you are not moving the humans at all (your code is not doing anything to move them as it is), the you do not want/need the enterframe function either...

                   

                  var humans = 90;

                   

                  for (i=0; i<humans; i++) { 

                         mcHuman = attachMovie("mcHuman", "mcHuman"+i, i);

                         mcHuman._x = random(Stage.width);

                         mcHuman._y = random(Stage.height);

                         mcHuman.xTarget = random(20)-10;

                         mcHuman.yTarget = random(20)-10;

                  }

                   

                  for (i=0; i<humans; i++) {

                       for (j=0; j<humans; j++) {

                              if (_root["mcHuman"+i].hitTest(_root["mcHuman"+j])) {

                                     trace("hit!");

                              }

                       }             

                  }

                  • 6. Re: Collision detection, multiple movieclips, self collision.
                    Ned Murphy Adobe Community Professional & MVP

                    One other thing that you left out that I showed you.  Unless you want to detect objects hitting themselves, which makes no sense to bother doing (because they always will), then you want to include the additional term I showed earlier in the conditional...

                     

                            if ( i != j && _root["mcHuman"+i].hitTest(_root["mcHuman"+j])) {

                    • 7. Re: Collision detection, multiple movieclips, self collision.
                      prodigalmaster Level 2

                      Ok. It makes sense, and yes I missed that i != j conditional.

                      Here is my whole code, which moves them around randomly/bounces off stage walls. But still doesn't trace "hit!" ever.

                       

                      var humans = 90;

                      for (i=0; i<humans; i++) {

                                mcHuman = attachMovie("mcHuman", "mcHuman"+i, i);

                                mcHuman._x = random(Stage.width);

                                mcHuman._y = random(Stage.height);

                                mcHuman.xTarget = random(20)-10;

                                mcHuman.yTarget = random(20)-10;

                      }

                      for (i=0; i<humans; i++) {

                                for (j=0; j<humans; j++) {

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

                                                    if (i != j && this.hitTest(_root["mcHuman"+j])) {

                                                              trace("hit!");

                                                    }

                                                    //this.swapDepths(this._y/(Stage.height/humans));//doesn't work

                                                    this._x += this.xTarget/8;//move mcHumans

                                                    this._y += this.yTarget/8;

                                                    if (this._x-this._width/2<=0) {

                                                              //Left barrier

                                                              this.xTarget = -this.xTarget;

                                                              this._x += this._width/6;

                                                    }

                                                    if (this._x+this._width/2>=Stage.width) {

                                                              //Right barrier

                                                              this.xTarget = -this.xTarget;

                                                              this._x -= this._width/6;

                                                    }

                                                    if (this._y-this._height/3<=0) {

                                                              //Top barrier

                                                              this.yTarget = -this.yTarget;

                                                              this._y += this._height/6;

                                                    }

                                                    if (this._y>=Stage.height) {

                                                              //Bottom barrier

                                                              this.yTarget = -this.yTarget;

                                                              this._y -= this._height/6;

                                                    }

                                          };

                                }

                      }

                      But I wasn't asking about the movement code.

                       

                      I can't figure out how to do collisions.

                      • 8. Re: Collision detection, multiple movieclips, self collision.
                        Ned Murphy Adobe Community Professional & MVP

                        You appear to have missed just about anything I said to do.  That code doesn't resemble what I showed you.  You do not want to be creating an onEnterFrame for each object in the loops that way.  Do the onEnterFrame and hit test the way I showed earlier (see response #3).

                        • 9. Re: Collision detection, multiple movieclips, self collision.
                          prodigalmaster Level 2

                          Thank you very much. It works now. Although higher numbers of mcHumans cause lag, but it's ok. Thank you very much.

                          Although im still confused about:

                          onEnterFrame = function {

                          }

                          having no () brackets. If I take em out is gives an error: Function name expected.

                           

                          btw, here is the 'final' code:

                           

                          var humans = 30;

                          for (i=0; i<humans; i++) {

                                    mcHuman = attachMovie("mcHuman", "mcHuman"+i, i);

                                    mcHuman._x = random(Stage.width);

                                    mcHuman._y = random(Stage.height);

                                    mcHuman.xTarget = random(20)-10;

                                    mcHuman.yTarget = random(20)-10;

                          }

                          this.onEnterFrame = function(){

                                    for (i=0; i<humans; i++) {

                                              for (j=0; j<humans; j++) {

                                                        if (i != j && _root["mcHuman"+i].hitTest(_root["mcHuman"+j])) {

                                                                  trace("hit!");

                                                        }

                                              }

                                              _root["mcHuman"+i]._x += _root["mcHuman"+i].xTarget/8;//move mcHumans

                                              _root["mcHuman"+i]._y += _root["mcHuman"+i].yTarget/8;

                                              if (_root["mcHuman"+i]._x-_root["mcHuman"+i]._width/2<=0) {

                                                        //Left barrier

                                                        _root["mcHuman"+i].xTarget = -_root["mcHuman"+i].xTarget;

                                                        _root["mcHuman"+i]._x += _root["mcHuman"+i]._width/6;

                                              }

                                              if (_root["mcHuman"+i]._x+_root["mcHuman"+i]._width/2>=Stage.width) {

                                                        //Right barrier

                                                        _root["mcHuman"+i].xTarget = -_root["mcHuman"+i].xTarget;

                                                        _root["mcHuman"+i]._x -= _root["mcHuman"+i]._width/6;

                                              }

                                              if (_root["mcHuman"+i]._y-_root["mcHuman"+i]._height/3<=0) {

                                                        //Top barrier

                                                        _root["mcHuman"+i].yTarget = -_root["mcHuman"+i].yTarget;

                                                        _root["mcHuman"+i]._y += _root["mcHuman"+i]._height/6;

                                              }

                                              if (_root["mcHuman"+i]._y>=Stage.height) {

                                                        //Bottom barrier

                                                        _root["mcHuman"+i].yTarget = -_root["mcHuman"+i].yTarget;

                                                        _root["mcHuman"+i]._y -= _root["mcHuman"+i]._height/6;

                                              }

                                    }

                                    //this.swapDepths(this._y/(Stage.height/humans));//doesn't work  

                          };

                           

                          • 10. Re: Collision detection, multiple movieclips, self collision.
                            Ned Murphy Adobe Community Professional & MVP

                            I see that I missed including the () in my earlier example.  Sorry about that.