PDA

View Full Version : Multiple HitTests Help! Please!



Unrealhacker12
May 24th, 2009, 01:24 PM
Hi there I've been reading up and looking at examples of how to handle multiple hittests. So far in my example I have failed.

///Variables
var Xdist:Number = 0;
var Ydist:Number = 0;
var partAng:Number = 0;
var bulletXvel:Number = 0;
var bulletYvel:Number = 0;
var bulletAcc:Number = 2;
var bulletDec:Number = 0.98;
var firePower:Number = 12;
///Arrays
//Stores Bullets
var particleArray:Array = [ ];
//Stores Atoms
var atomArray:Array = [ ];
//Stores the individual Atom's X'n'Y Position
var atomPosX:Array = [600,200];
var atomPosY:Array = [150,150];

//Creates the Atoms using MC from the library and X'n'Y Pos Arrays, then stores the created in an array.
for(var i=0; i<atomPosX.length; i++){
var nuke = new atom();

nuke.x = atomPosX[i];
nuke.y = atomPosY[i];

atomArray.push(nuke);
addChild(nuke);
}

stage.addEventListener(Event.ENTER_FRAME, eachFrame);
function eachFrame(event:Event):void {
//Trig for barrel position
Xdist = (turret.x - mouseX);
Ydist = (mouseY - turret.y);
turret.barrel.rotation = -((Math.atan2(Ydist,Xdist)) * (180/Math.PI));
//Move Particles
for(var i=0; i<particleArray.length; i++) {
//Temporary name storing var
var part = particleArray[i];
//Moves the bullet by its individual velocities
part.x += part.xmov;
part.y += part.ymov;
//Removes bullets when they hit the bounds of the stage
if(part.x > 850 || part.x < -50 || part.y < -50 || part.y > 650) {
removeChild(part);
particleArray.splice(i, 1);
}
}
}
stage.addEventListener(MouseEvent.MOUSE_DOWN, fireParticle);
function fireParticle(event:MouseEvent):void {
var part = new electron();

partAng = turret.barrel.rotation;
//Intial X'n'Y Position of the bullet
part.x = turret.x+(70* Math.cos((turret.barrel.rotation+180)* Math.PI/180));
part.y = turret.y+(70* Math.sin((turret.barrel.rotation+180)* Math.PI/180));
//Initial bullet velocities.
part.xmov = -Math.cos(partAng*Math.PI/180)* firePower;
part.ymov = -Math.sin(partAng*Math.PI/180)* firePower;
//Storing bullet in the array;
particleArray.push(part);
addChild(part);
}

Basically I want to be able to check for a hitTest against all the bullets on stage against all atoms created. I attempted to do this with two for loops and it didn't work can anyone help me with this specific problem. Project folder attached. :crying:

Thanks in advance.

cbeech
May 24th, 2009, 02:55 PM
hmmm looks like you could do it here:


function eachFrame(event:Event):void {
//Trig for barrel position
Xdist = (turret.x - mouseX);
Ydist = (mouseY - turret.y);
turret.barrel.rotation = -((Math.atan2(Ydist,Xdist)) * (180/Math.PI));
//Move Particles
for(var i=0; i<particleArray.length; i++) {
//Temporary name storing var
var part = particleArray[i];
//Moves the bullet by its individual velocities
part.x += part.xmov;
part.y += part.ymov;
//Removes bullets when they hit the bounds of the stage
if(part.x > 850 || part.x < -50 || part.y < -50 || part.y > 650) {
removeChild(part);
particleArray.splice(i, 1);
}
//Check this particle against the atoms
for(var a=0; a<atomArray.length; a++) {
if(part.hitTest(atomArray[a]) {
//do your stuff
break;
}
}
}
}

Unrealhacker12
May 24th, 2009, 03:40 PM
Cool dude works although it should look something like this as you made mistakes THANK YOU!

//Check this particle against the atoms
for(var a=0; a<atomArray.length; a++) {
if(atomArray[a].hitTestPoint(part.x,part.y,true)) {
trace("works");
break;
}
}

Unrealhacker12
May 24th, 2009, 03:43 PM
Also what's the break; for I thought that was only used in switch statements?

cbeech
May 24th, 2009, 04:58 PM
LMAO - yes i did not use 'hitTestObject' - just typing off the top of my head :D however you also did not mention that you would like to use a shape based test ;) would you like a bitmap shape based test instead? i could easily test the entire container... LOL (mistakes) :D

break; can also be used in loop, both while and for - you can also target a break to a 'parent' loop to gain further control if you use a label ;)

it will stop the loop from executing at that point - since the atomArray check is nested - i assumed that if a hit occurs you would want to 'blow up' the instances and remove them, then you would kill the loop at that point since there is no reason to check further for that instance - additionally if you were to do so, you'll probably hit an error since the instance will have been removed by that time. by breaking out of the nested loop after destroying the instances, you would still want to continue iteration of the particle loop check all other instances, even though 'one' had made a hit.