11 Replies Latest reply on Nov 22, 2006 4:07 PM by Jimmathy

    hitTesting

    Jimmathy Level 1
      In order to save time, i would like to script this game in such a way that i can put some code(similar to:
      if(this.hitTest(_root.MC)){ ) on MC#1 but make the other clip being tested any other MC on the stage with out coding separate hitTests for each. In other words i want to make it so whenever MC#1 collides with ANY other MC the action will perform. I havent the foggiest idea on how this would be done. Your help is much appreciated.

      thanks
        • 1. Re: hitTesting
          shikozel Level 1
          you will need a script that check the hitTest every frame. somthing like that:

          function onEnterFrame(){
          checkHitTest(MC#1)
          }

          function checkHitTest(mc:MovieClip):Void{
          for(var i=0;i<numOfMovies;i++){
          if (mc.hitTest(this["mc#"+i])){

          trace("you got a hit")

          }
          }

          }
          • 2. Re: hitTesting
            Jimmathy Level 1
            this would be attaching it to a frame. For my purposes, it would be easier to attach it to a MC. How could this script be modified to fit in a MC and to tell if it collided with anything at all?

            thanks
            • 3. Re: hitTesting
              Level 7
              shikozel,

              > you will need a script that check the hitTest every frame.

              The MovieClip.hitTest() frequency is ultimately up to you. If your game
              moves quickly, such as Pac-Man, you may indeed want to check on every frame
              or with setInterval() at a short interval, such as every 50 milliseconds.
              If your game moves slowly, such as Theseus and the Minotaur, you may only
              have to check once per move.

              > function checkHitTest(mc:MovieClip):Void{
              > for(var i=0;i<numOfMovies;i++){
              > if (mc.hitTest(this["mc#"+i])){
              ...

              Looping through an array is certainly one way to go. If you're checking
              an array of 250 movie clips at the default frame rate of your movie
              (approximately 12 times a second), you'll be checking for collisions with
              3000 objects a second. That's a lot to ask!

              Of course, you may have far fewer movie clips to check against -- you
              really didn't say -- so you'll have to experiment. You *could* put all the
              *other* movie clips into a single parent clip (a container clip) and check
              for a collision against that alone. That's worth a try.


              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Re: hitTesting
                Level 7
                jimmathy,

                I recently finished a project where I had to use _droptarget, but the
                issue is that my targets were actually under another movieclip,
                therefore they were never able to populate that droptarget property.

                I developed a system where my dragged mc was sending an event through a
                class associated with ASBroacaster. All my fictional targets were
                registered to that class as listeners. So "onDrop" they all did a
                hittest and were populating an array back into my dragged mc, where I
                execute the appropriate methods given the result.

                JG


                Jimmathy wrote:
                > this would be attaching it to a frame. For my purposes, it would be easier to
                > attach it to a MC. How could this script be modified to fit in a MC and to tell
                > if it collided with anything at all?
                >
                > thanks
                >
                >
                • 5. Re: hitTesting
                  shikozel Level 1
                  Jimmathy,

                  it is not a good practice to place your code on a movie clip. but if you decided to do it anyway it should look like that:

                  //asuuming all you MC is on the root timeline

                  onClipEvent(enterFrame){
                  for(var i=0;i<numOfMovies;i++){
                  if (this.hitTest(_root["mc#"+i])){
                  }

                  }
                  • 6. Re: hitTesting
                    shikozel Level 1
                    You right in everything David but he didnt specify any of that
                    • 7. Re: hitTesting
                      Level 7
                      shikozel,

                      > You right in everything David but he didnt specify any of that

                      Exactly. ;) That's why I mentioned the alternatives -- because the OP
                      didn't specify rapid collision testing, yet you replied "you will need a
                      script that check the hitTest every frame".


                      David Stiller
                      Adobe Community Expert
                      Dev blog, http://www.quip.net/blog/
                      "Luck is the residue of good design."


                      • 8. Re: hitTesting
                        Jimmathy Level 1
                        Okay, this appears that it will work. What are the potential problems of putting script into a MC? Why is it not a good practice? Is it because you cannot attach movies with actionscript ect. via actionscript?

                        thanks
                        • 9. hitTesting
                          Jimmathy Level 1
                          how can i get you guys my .fla to show you what im trying to do? Essentially i have a total of 6 cars rotating at random speeds around the center a 3 lane roundabout(2 cars per lane). Each car is actually a MC in side of a MC placed off to the right of the center point in order to get it into the lane that it belongs. The problem obviously with my random speeds (well actually random *3) is that the cars "drive" over and under one and other. Therefore I was going to give each of the MC's(which i have been scripting seperately) a line of coding that performs a hitTest with any other MC on the stage. Then if the hitTest is positive, it would recalculate the number of degrees rotated each frame(60 fps), so that the cars would slow up/down in order to clear up traffic around the roundabout. As of this point, I have each of the cars coded as following:

                          onClipEvent(load){
                          speed= Math.random()*3;
                          }

                          onClipEvent(enterframe){
                          this._rotation -=speed;
                          }

                          So, do you think my system of coding will work for multiple levels of the game, when you escape the terrifying roundabout, or should I attempt to rescript it via the frames so that i can make each frame a new level?

                          what do you think?
                          • 10. Re: hitTesting
                            shikozel Level 1
                            It is not a good practice because you want to have your code only in one place. When you put a script on the MC it is very hard to understand, debug or modify. Lets say you want to modify your script in 2 months. Will you remember where did you place your script and on what MC??? It is easier to have a separate AS file where you can read the entire script without searching among movie clips
                            • 11. Re: hitTesting
                              Jimmathy Level 1
                              i see, sounds reasonable.