Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Collision within movie clip

Status
Not open for further replies.

XLax

Technical User
May 22, 2006
25
CA
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.

Kenneth Kawamoto
 
I see what you mean and I will try it

I beleive that you've inspired me to use hitTest in a completely different way for some things i've been trying to do.

Do you think you could explain how I'd use the array though?
 
I have just done the test and it worked well.

You need to go around the edge of the shape with the mouse and make notes of x and y coordinates (with a pen and a paper!), then put them in an array.

My array looks like this:

var arrayPoints:Array = [[22, 0], [41, 6], [52, 24], [52, 51], [36, 73], [14, 79], [2, 62], [6, 39], [0, 16], [7, 0]];

So now I have coordinates of 10 points around the edge of the shape.

Just go through them and see if any of the points in the array is inside another shape.

(Flash 8 BitmapData.hitTest is FAR EASIER than this, by the way...)

Kenneth Kawamoto
 
Seems my action script skills are rusty... I used to know how to use arrays in functions

Can you remind me how to use an array in a hit test function?

(I wish I had a more up to date flash but I can't offord it lol)
 
[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.

Kenneth Kawamoto
 
Woot!

(check out my signature)
 
There.

Alex -
Kawamoto fan
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top