Diagram:
//
on stage:
BOX
MOVIE CLIP WITH CIRCLE IN IT
//
How would I write a script to see if the box and circle collide?
(If possible, using shape flag)
If you are on Flash 8 you can use BitmapData.hitTest method, which allows you to perform hitTest on two shapes (rather than bounding boxes).
But if you are on Flash 7 or older, it's going to be a bit tricky, because you only have traditional hitTest(), which cannot test two shapes but can only test a point against a shape.
If at least one of the shapes is a geometric shape (square, circle, etc) you can determine the x-y coordinates of all the points around the edge of the shape using mathematics. Then you can go through those points to see if they pass the hitTest.
If both shapes are irregular shapes, I would store the location of the points around the edge in an array, then go through them to see if they pass the hitTest. Actually, this would be faster than the method above, as you can limit the number of points stored in the array so the hitTest loop can be much shorter.
[tt]// main timeline
var arrayPoints:Array = [[22, 0], [41, 6], [52, 24], [52, 51], [36, 73], [14, 79], [2, 62], [6, 39], [0, 16], [7, 0]];
var pointsCnt:Number = arrayPoints.length;
//
stop();
//
onEnterFrame = function ():Void {
for (var i = 0; i<pointsCnt; i++) {
var ptX:Number = arrayPoints[0]+mcB._x;
var ptY:Number = arrayPoints[1]+mcB._y;
if (mcA.hitTest(ptX, ptY, true)) {
trace("Hit!");
return;
}
}
trace("No hit");
};
//[/tt]
There are two MovieClips "mcA" and "mcB" on Stage. "arrayPoints" contains points on the outline of mcB.
My actual script had more in it to make mcB draggable - but I removed them for clarification.
Please remove all the type declarations (":Array" etc) if you're using AS1.
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.