11 Replies Latest reply on Jan 26, 2007 8:43 AM by gigisfarleaza

    hitTest help

    gigisfarleaza
      Hi

      I'm trying to make a little game: I have a movie "bird" and i duplicate this movie randomly and they are moving around the scene for me to shoot them down. i have created an array with an id for each bird.
      On every bullet i have the following sccript


      I verify for every clip if the bullet hits one of my birds that arr named ["p"+number], but sometimes probably when the cpu is busy it doesn't work.

      isn't there a way to do the hitTest without using for ?
        • 1. hitTest help
          abeall Level 3
          1) Not clear what the problem is

          2) You have to use for loop if you want to detect hitTest on idividual objects, but you could also put all your objects in a container clip and hitTest once against the whole containter. If there's a hit, you can then check on all the containing clips, and break on the one thats hit. Much more efficient that way. Also use for(var i in arr) instead, its a bit faster.

          3) Creating a local reference will speed things up:
          var p = _root["p"+_root.arr[ i ]];
          if (p.hitTest(this)) {
          _root.punctaj += p.points;
          p.removeMovieClip();
          _parent.removeMovieClip();
          }

          4) updateAfterEvent() is useless in this context.
          • 2. Re: hitTest help
            kglad Adobe Community Professional & MVP
            you setup is inefficient and probably causing quite a load on the cpu. you can do several things to improve performance so the cpu isn't stressed.

            if you're using flash 8 enable the cacheAsBitmap property of each of your bullets and each of your birds (unless you're doing some scaling, alpha fading or timeline changes of the bullets' or birds' movieclips).

            store your bullets in another array. then you'll only need one enterFrame loop in your entire project to check whether any existing bullets have hit any existing birds. when birds or bullets no longer need to be checked in your hitTest remove them from their array and remove them.
            • 3. Re: hitTest help
              gigisfarleaza Level 1
              Thanks all for the help !
              • 4. Re: hitTest help
                gigisfarleaza Level 1
                "but you could also put all your objects in a container clip and hitTest once against the whole containter. If there's a hit, you can then check on all the containing clips, and break on the one thats hit."

                abeall can u help me with this pls ?

                i duplicate all the bird in a movie clip but i don't know how to make the hitTest work

                tks again for all the help!
                • 5. Re: hitTest help
                  abeall Level 3
                  something like

                  if(container_mc.hitTest(this._x,this._y,true)){
                  for(var i in arr){
                  var p = _root["p"+_root.arr[ i ]];
                  if (p.hitTest(this)) {
                  _root.punctaj += p.points;
                  p.removeMovieClip();
                  _parent.removeMovieClip();
                  break;
                  }
                  }
                  }
                  • 6. Re: hitTest help
                    gigisfarleaza Level 1
                    i tried this code

                    onClipEvent (enterFrame) {
                    if (_root.birds.hitTest(this._x, this._y, true)) {
                    trace("da");
                    }
                    }
                    and is not entering if never

                    an if i try like this
                    onClipEvent (enterFrame) {
                    if (_root.birds.hitTest(this)) {
                    trace("da");
                    }
                    }
                    the hitTest is always true
                    • 7. Re: hitTest help
                      abeall Level 3
                      > and is not entering if never
                      How big are your bullets and how fast are they moving? This hitTest is checking the X,Y location of the bullet with another clip, and using the target clip outline.

                      The reason your second example aways returns true is because when you hitTest movieclip-to-movieclip it uses both clips' bounding box, not actual shape.

                      Are you using Flash 8?
                      • 8. Re: hitTest help
                        gigisfarleaza Level 1
                        yes I am using flash 8

                        the bullets are small 4x4 and they are moving fast
                        • 9. hitTest help
                          gigisfarleaza Level 1
                          can it be because the bullets arr in another movie ?
                          I've tried using localToGlobal(this._x).... but its still doesn't work ...
                          • 10. Re: hitTest help
                            abeall Level 3
                            > can it be because the bullets arr in another movie ?
                            Yes, that would be a problem if your bullets are in a container, and the bullet container is not at the same coordinates as the birds container. You're on the right track with localToGlobal. Can I see your localToGlobal code? Alternatively, you could just mak sure your bullet container and bird container are at the exact same location.
                            • 11. Re: hitTest help
                              gigisfarleaza Level 1
                              i don't really know how to use localToGlobal, i tried localToGlobal(this._x) but it's not working, but i-l try putting them at the same coordinates. tks again abeall