4 Replies Latest reply on Oct 7, 2008 1:18 PM by Rothrock

    HitTest Problems!!

    chinmayagoyal
      Hello,
      I have an object(car) on the stage which can be controlled with the arrow keys. I have created 4 points around the boundary of the object(small circles), one upwards, second downwards and so on in all directions.(placed within mc "car")
      The problem is that I want to do a hittest between a object and the small circles which are within the mc car. I have used the code:

      onEnterFrame=function(){
      if(car.hitTest(object.upcircle._x,object.upcircle. _y,true)){
      //CODE//
      }
      }
      This code doesn't work (this is for collision between the center of the circle and the object), but, what's odd is that the normal boundary hitTest i.e
      onEnterFrame=function(){
      if(car.hitTest(object.upcircle)){
      //CODE//
      }
      }

      THIS WORKS!!!!!
      PLEASE HELP........
      Thanking You,
      Chinmaya

      P.S. This code is written in the 1st frame (My flash document currently has one frame)
        • 1. Re: HitTest Problems!!
          kglad Adobe Community Professional & MVP
          use:

          • 2. Re: HitTest Problems!!
            Rothrock Level 5
            That doesn't seem like it is what you want in any event. If the little circles are inside the car clip, then why would you be hitTesting them with the car clip? The second version will always be hitting because the bounding box of the circle will always be inside the bounding box of the car since the circle is inside the car!

            The first one is failing for a different reason, I think. In this case it is a problem of frame of reference and/or scope. First if you say the circles are inside the car clip then you need to reference them as car.object.upcircle or car.upcircle.

            Next is that object.upcircle._x is going to give the _x value of upcircle RELATIVE TO OBJECT not the stage. So try this using your first bit of code:

            trace("x: "+object.upcircle._x);

            You should see that it isn't ever changing (unless you have code to change the position of upcircle inside of its parent object). So I'm guessing the circles are fairly near the origin of the clip they are put into. So if you drive your car into the upper left corner of the screen it should magically work for some small number of spots!

            But given that you probably want it to work in more than the upper left corner...then you need to convert the local coordinates to global. Check out localToGlobal() or you can just use a little math.

            object._x+object.upcircle._x

            Should work. I'm going to give you some advice here. I wouldn't call them upcirlce, downcircle, etc. What happens if you realize you need 6 of these or 7 to give a good predictive hit? Instead I would give then names like:

            hitSpot0, hitSpot1, etc.

            You could then use a little loop to go through all of them instead of having to type out different names for each one. Just a thought.
            • 3. HitTest Problems!!
              chinmayagoyal Level 1
              Hello,
              I am very very sorry Rothrock, Here is the code edited,
              onEnterFrame=function(){
              if(object.hitTest(car.upcircle._x,car.upcircle. _y,true)){
              //CODE//
              }
              }

              The object is a movie clip in which a track has been carved.
              Sorry once again,
              Can you help me out now,
              Thanks,
              Chinmaya
              P.S. The circles are within car mc.
              EDIT: Thanks for your quick reply's
              • 4. Re: HitTest Problems!!
                Rothrock Level 5
                Sure thing. Just skip the first sentence of my previous post.

                Trace out car.upcircle._x and you will see that it always stays the same because upcircle isn't moving inside of the car clip.

                Change the code to

                car._x + car.upcircle._x

                and that should work. Not an elegant solution but workable.

                I still recomment changing the names of your hit dots.

                Check out localToGlobal in the help files.