Search code examples
actionscript-3flashactionscriptflash-cs6hittest

AS3 hitTestObject not working at all


I've been working on a large project in Flash CS6 and ActionScript 3 which includes jigsaws etc. I have been using hitTestObject throughout the project and it had been working fine, then all of a sudden it stopped working. It won't work in any new instances or even where it worked before. I created a small test piece, and this isn't working either:

   import flash.events.MouseEvent;

   hit_txt.visible = false;

   object2_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);

   function drag(e:MouseEvent = null):void {
      object2_mc.startDrag();
   } 

   if (object2_mc.hitTestObject(object1_mc)) {
      hit_txt.visible = true;
   }

Any help or suggestions would be greatly appreciated, thanks in advance.


Solution

  • You are not stating when to do the checking of the hit test. So either you check by enter frame, or while you move the object

    object2_mc.addEventListener(MouseEvent.MOUSE_MOVE, move);
    function move(e:MouseEvent = null):void
    {
        if (object2_mc.hitTestObject(object1_mc))
        {
            hit_txt.visible = true;
        }
    }