Flash Helps: Flash AS3 hitTestObject (hitTest) and startDrag

There is a slight change in AS3 (actionScript 3) with the AS2 method of hitTest().

In AS3 it is now hitTestObject(). What hitTestObject does is it evaluates the display object (or MovieClip) to see if it overlaps or intersects with the obj display object. So you would use something like:

myMC1.hitTestObject(myMC2);

In this hitTestObject() simple example below, I have two movieclips on the stage named sq1 and sq2. When you drag sq2 to sq1 it traces true. The actionscript is as follows:

import flash.display.Sprite;
import flash.events.MouseEvent;

sq2.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown)

function mouseDown(event:MouseEvent):void {
sq2.startDrag();

}
sq2.addEventListener(MouseEvent.MOUSE_UP, mouseReleased);

function mouseReleased(event:MouseEvent):void {
sq2.stopDrag();
trace(sq2.hitTestObject(sq1));
//trace true or false if sq2 is on sq1
}

Sample Files:
hitTestDrag.fla
hitTestDrag.swf

Related Posts:

  • No Related Posts

Comments are closed.