stage.addEventListener(MouseEvent.MOUSE_MOVE, rectMove);
stage.addEventListener(Event.ENTER_FRAME, hitTest);
function rectMove(event:MouseEvent)
{
rect1.x = mouseX;
rect1.y = mouseY;
}
function hitTest(event:Event)
{
if(rect1.hitTestObject(rect2))
{
txt_hit.text = "HIT!";
}
else
{
txt_hit.text = "";
}
}
Perfect hitTest-ing! If you test the movie you can see it detects collision like a charm!
stage.addEventListener(Event.ENTER_FRAME, hitTest);
function hitTest(event:Event)
{
if(rect1.hitTestPoint(mouseX,mouseY,true))
{
txt_hit.text = "HIT!";
}
else
{
txt_hit.text = "";
}
}
Now if you test the movie even if your mouse is way inside the bounding box if it's not over a painted area, it doesn't trigger a hit. But the instant you move it on a painted area, you get a "HIT!".
stage.addEventListener(Event.ENTER_FRAME, hitTest);
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveMouse);
function moveMouse(event:MouseEvent)
{
mouse.x = mouseX;
mouse.y = mouseY;
}
function hitTest(event:Event)
{
if(rect1.hitTestPoint(mouse.dot.x,mouse.dot.y,true))
{
txt_hit.text = "HIT!";
}
else
{
txt_hit.text = "";
}
}
If you test the movie, you will see it doesn't work. Trace the coordinates you're passing to the hitTestPoint and you'll see the only possible way for it to work is to move the object to the top left corner. Definately not what we want. And that's because those coordinates are relative to the parent of the dot object, which is the mouse object. To fix this, convert them to global coordinates using localToGlobal function. Replace the if condition with this:
var cursor:Point = new Point(mouse.dot.x,mouse.dot.y);
if(rect1.hitTestPoint(mouse.localToGlobal(cursor).x,mouse.localToGlobal(cursor).y,true))
{
txt_hit.text = "HIT!";
}
else
{
txt_hit.text = "";
}
If this is a bit advanced to you, what you do is create a point (still in relative space to mouse object) with the dot's x and y coordinates and then pass it to localToGlobal, which returns a point in absolute space, which x and y coordinates you use for the hitTestPoint.
//code usage: hitTesting.isHitting(first object,second object,first upper left corner(Point),second upper left corner(Point),precision)
//simple code:
hitTesting.isHitting(mc_obj1,mc_obj2,new Point(mc_obj1.x,mc_obj1.y),new Point(mc_obj2.x,mc_obj2.y),5)
Note: The two points have to be the upper left corner of the objects, caused me a lot of trouble when I passed the objects x and y when they were with central registration. If you want to use Objects with their register points in the center you can use new Point(object.x-object.widtpackage
{
import flash.geom.Point;
import flash.geom.Rectangle;
public class hitTesting
{
public static function isHitting(hitter:Object,hittee:Object,upper1:Point,upper2:Point,precision:Number):Boolean
{
if(hittee.hitTestObject(hitter))
{
return searchCommon(hitter,hittee,getRectangle(hitter,hittee,upper1,upper2),precision)
}
else
{
return false;
}
}
static function getRectangle(object1:Object,object2:Object,upper1:Point,upper2:Point):Rectangle
{
var returnRectangle:Rectangle;
if(upper1.x <= upper2.x)
{
if(upper2.y >= upper1.y)
{
returnRectangle = new Rectangle(upper2.x,upper2.y,(upper1.x object1.width)-upper2.x,(upper1.y object1.height)-upper2.y);
}
else
{
returnRectangle = new Rectangle(upper2.x,upper1.y,(upper1.x object1.width)-upper2.x,(upper2.y object2.height)-upper1.y);
}
}
else
{
if(upper2.y >= upper1.y)
{
returnRectangle = new Rectangle(upper1.x,upper2.y,(upper2.x object2.width)-upper1.x,(upper1.y object1.height)-upper2.y);
}
else
{
returnRectangle = new Rectangle(upper1.x,upper1.y,(upper2.x object2.width)-upper1.x,(upper2.y object2.height)-upper1.y);
}
}
return returnRectangle;
}
static function searchCommon(object1:Object,object2:Object,rect:Rectangle,precision:Number):Boolean
{
var checker:Point;
var returnValue:Boolean = false;
for(var j = 0;j < Math.floor(rect.height/precision);j )
{
if(!returnValue)
{
for(var i=0;i < Math.floor(rect.width/precision);i )
{
if(!returnValue)
{
checker = new Point(rect.x i*precision,rect.y j*precision);
if(object1.hitTestPoint(checker.x,checker.y,true) && object2.hitTestPoint(checker.x,checker.y,true))
{
returnValue=true;
}
}
else
{
break;
}
}
}
else
{
break;
}
}
return returnValue
}
}
}
I hope I helped someone with all of this information.
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented: