PDA

View Full Version : [AS3] hitTest of multiple objects at same coords


deleeuw
08-30-2008, 09:11 PM
Hey all,
Making a game where there units have the ability to "stack" (i.e be at the same x,y coords). How does one do a hitTest/Collision Detect that detects all the stacked units?

Thx

syruplord
08-31-2008, 01:49 AM
for loops

let's say you have box0, box2, box3, box4...box 99;
you can do a for loop to do a hittest between all of them.

for(var i = 0; i<100; i++){
var mc1 = _root["box"+i];
for(var j=0; j<100; j++){
var mc2 = _root["box"+j];
//hit test if the first box is not the same as the 2nd box
if(mc1 != mc2){
//hit test code
}
}
}

deleeuw
08-31-2008, 02:33 AM
Thanks syruplord, although I think the code example is in AS2, not AS3. Can I
assume that it's essentially the same principle in AS3.

Thx

deleeuw
09-01-2008, 08:33 PM
I think I may have found a solution, but I'm not sure if its the most efficient (given that there'll be about 150 objects):


for (k=0; k<numChildren; k++) {
if (getChildAt(k).hitTestPoint(mouseX,mouseY,true)){
trace(getChildAt(k).name);
}
}


Is this really the best way? Wouldn't it slow down doing the loop/

Thx