7 Replies Latest reply on May 11, 2011 7:45 AM by kglad

    Collision detection / one movie clip following another

    HDBurdation

      I'm working on a side-scrolling space shooter game, and I've hit a snag:  The rocketship movie clip has two nested layers, one for the ship itself and one for an animated cycle of the flames coming out from behind.  The ship loses health when it collides with an enemy ship, but since both the body of the ship and the flames are part of the same clip, the ship, of course, also loses health when the flames hit an enemy ship.  What I'm trying to do is alter it so the flames will destroy the enemy ships, but the player won't lose any health.  My first inclination was to separate the flames into their own clip and simply attach it to the ship -- i.e., the flames clip is basically parented to the ship, on a layer underneath, but is controlled by a separate script than that of the ship itself.  The flames would be aligned with the ship, and when the ship moves, the flames move.  Simple enough.  So far, I've found no successful way to do this.  I searched for alternatives (masking, etc., etc.) to no avail.  I'm working in Flash 8, AS2.  Every movie clip has its own class.  I can post some code if need be.  Any advice, or simply a point in the right direction, would be greatly appreciated.  Thanks!

        • 1. Re: Collision detection / one movie clip following another
          kglad Adobe Community Professional & MVP

          copy the frames of your flame animation and paste them into a new movieclip (eg, flame mc).  remove the flame animation from your rocket ship.  add flame mc to your rocketship movieclip and assign an instance name (eg, flame_mc).  convert the body of your rocketship to a movieclip and assign an instance name (eg, body_mc).

           

          whenever you have a positive hittest with your rocketship (eg, rocketship_mc) you can check if rocketship_mc.body_mc and/or rocketship_mc.flames_mc have a positive hittest and take appropriate action.

          1 person found this helpful
          • 2. Re: Collision detection / one movie clip following another
            HDBurdation Level 1

            kglad, thanks for the info.  I made the ship body and flames their own movieclips, and I was able to connect the enemy hitTest to only the body, but couldn't figure out a separate hitTest for the flames.  So, the ship doesn't lose health when the flames touch the enemies, but the enemies aren't destroyed.  The hitTest code that tells the enemy to explode and subtract health from the ship when they collide is in the enemy class.  Here's the code, within the onEnterFrame function:

             

            }
                    if (this.hitTest(_root.ship.shipBody))
                    {
                        if(_root.ship.shield._visible == false)
                        {
                        _root.ship.updateHealth(-20);
                        }
                        explode();
                    }

             

            I though I'd just be able to copy and paste the code right below this and modify it, but it didn't work.  Did I just put it in the wrong spot?  Do I need to modify the above code instead of adding a whole new string?  I tried creating a new class for the flames, but that didn't seem to do anything either.  Please bear with me... I'm still pretty new at all this ActionScript business.  Thanks!

            • 3. Re: Collision detection / one movie clip following another
              kglad Adobe Community Professional & MVP

              use:

               


               

              }
                      if (this.hitTest(_root.ship.shipBody))
                      {
                          if(_root.ship.shield._visible == false)
                          {
                          _root.ship.updateHealth(-20);
                          }
                          explode();
                      }

               

              if(this.hitTest(_root.ship.shipFlames){  // assuming shipFlames is correct

              explode();  // assuming you want the enemy to explore from flame contact

              }

              • 4. Re: Collision detection / one movie clip following another
                HDBurdation Level 1

                Works!  Totally awesome.  I tried something similar to that, although now I can't remember what the exact code was.  I think I had some misplaced brackets somewhere.  Thank you very much!

                • 5. Re: Collision detection / one movie clip following another
                  kglad Adobe Community Professional & MVP

                  you're welcome.

                   

                  p.s.  in the future, please mark helpful/correct responses, if there are any.

                  • 6. Re: Collision detection / one movie clip following another
                    HDBurdation Level 1

                    Sorry.  I guess I didn't even notice that.  This was my first post here, I was in a hurry, and I've been running on very minimal sleep.  Fixed!