AS3 HitTest Checks on ActionScript 3.0
This is an example on how one should do a hittest in ActionScript 3.0.
This is an example of plain simple flash mode, where MovieClipA(red) and MovieClipB(blue) – Red being draggable and blue square listens to the a HitTest condition.
Some criteria needed for this task :-
- Two movieclips – Red and Blue
- EventListener assigned to Red to have it drag and drop able.
- EventListener for EnterFrame for Blue to constantly checks for HitTest condition
For those new in EventListener in ActionScript 3.0 here’s a good tutorial for it.
See sample below :-
Download sample source code here.
//Task to accomplish for HitTest //1. Two movieclips - Red and Blue (Given instance name MovieClipRed and MovieClipBlue //2. EventListener assigned to Red to have it drag and drop able. //3. EventListener for EnterFrame for Blue to constantly checks for HitTest condition //Task 1. is done in the stage itself. //Task 2. Place an Eventlistener on Red have it drag nad drop able. MovieClipRed.addEventListener(MouseEvent.MOUSE_DOWN,startDragme); MovieClipRed.addEventListener(MouseEvent.MOUSE_UP,stopDragme); // Below is the trigger function for EventListener Task 2. function startDragme(event:Event):void{ MovieClipRed.startDrag(true); StatusTxt.text = "Dragging Red"; } function stopDragme(event:Event):void{ MovieClipRed.stopDrag(); StatusTxt.text = "Dropping Red"; } MovieClipBlue.addEventListener(Event.ENTER_FRAME,checkHitTest); function checkHitTest(event:Event){ if(MovieClipRed.hitTestObject(MovieClipBlue)) { StatusTxt.text = "Bingo - Blue has detected Red"; } }





Re: Hit Test Tutorial
How does the code go once the red button is over the blue button so it locks in the exact spot of the blue and you can’t move it anymore?
Cheers Liz
Hiya liz, just if hittest is true; do this assign coordinate of MovieClipRed’s x and y to MovieClipBlue.x and MovieClipBlue.y this will make blue snap perfectly on to the red. Then, MovieClipBlue.removeListener() on the blue clip to stop it from being dragged another
Hi
I’ve copied the above code into Flash CS5.
It works, only when I let go of the red square, it stays stuck to the mouse pointer.
I can’t see a problem with your code.
Any ideas why this is happening?
Cheers
Shaun
import caurina.transitions.*;
import flash.events.KeyboardEvent;
var t: targ1 = new targ1();
t.x = 340;
t.y = 145;
addChild(t);
var t2: targ2 = new targ2();
t2.x = 800;
t2.y = 600;
addChild(t2);
var s: symbol1 = new symbol1();
s.x = 50;
s.y = 50;
addChild(s);
var s2: symbol2 = new symbol2();
s2.x = 500;
s2.y = 500;
addChild(s2);
function beginDrag(e:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_MOVE,moveMC);
stage.addEventListener(MouseEvent.MOUSE_UP,endDrag);
}
s.addEventListener(MouseEvent.MOUSE_DOWN,beginDrag);
s2.addEventListener(MouseEvent.MOUSE_DOWN,beginDrag);
function endDrag(e:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE,moveMC);
if (s.hitTestObject(t)) {
Tweener.addTween(s,{x:340, y:145, time:.5, transition:”easeIn”});
} else {
Tweener.addTween(s,{x:mouseX, y:mouseY, time:1, transition:”easeIn”});
}
if (s2.hitTestObject(t2)) {
Tweener.addTween(s2,{x:800, y:600, time:.5, transition:”easeIn”});
} else {
Tweener.addTween(s2,{x:mouseX, y:mouseY, time:1, transition:”easeIn”});
}
stage.removeEventListener(MouseEvent.MOUSE_UP,endDrag);
}
function moveMC(e:MouseEvent):void {
Tweener.addTween(s,{x:mouseX,y:mouseY,time:10,transition:”easIn”});
e.updateAfterEvent();
Tweener.addTween(s2,{x:mouseX,y:mouseY,time:10,transition:”easIn”});
e.updateAfterEvent();
}
stop();
i have a probleme with my code…
if i want to drag one of my object, both are moving …
someone has an idea ?