PDA

View Full Version : [AS3] Collision detection issue.


SonOfChaos
06-27-2009, 09:52 AM
Okay, I've got a healthbar that shrinks everytime a character is hit when an object becomes larger than a certain size. This is in an enter_frame, basically the healthbar shrinks every frame, when I want it to shrink ONCE and then stop until hit again. I used a boolean value set to false to control it so I thought. Argh.The current behavior is that is shrinks, stops, and then shrinks again, like the boolean variable turning itself off then on repeatedly quickly. I've tried moving the else statement to no avail.



collisionOne is a boolean that = false;


//This is in an enterframe event.
for(var e=0; e<boxArray.length; e++){
if(boxArray[e].scaleX > 2){

boxArray[e].die();
if(! collisionOne){
trace('collision One');
hbar.width -=5;
collisionOne = true;
} else {
collisionOne = false;
}
}


}

attunedesigns
06-27-2009, 12:44 PM
Thats because it is turning it off and on. You are catching both true and false, and switching for each condition.

tadster
06-27-2009, 01:00 PM
this is the second time i'm answering this question.....
i also showed you this on the other site you asked it on. (just so you know)

collisionOne should only be based on if the charecter hits the object.



function enterframefunc(e:Event):void
{

if (charecter.hitTestObject(theobject)) {collisionOne = true} else {collisionOne = false}

for(var e=0; e<boxArray.length; e++){
if(boxArray[e].scaleX > 2){

if(collisionOne) {hbar.width -=5;break;}

}
}

}



the way you have it, if more than one object in boxArray has a scaleX > 2 then the hbar would shrink more than once, you need the break, not boxArray[e].die()

hope this helps

SonOfChaos
06-27-2009, 06:31 PM
Yes I'm using multiple sites.

My problem is that their is NO hit detection here, only a scaleX property that is being monitored for when to pass damage.

I can't get the boolean to swap states and stay that way cause the enter frame keeps passing the state.

I tried removing it from the for loop to no avail.


for(var e=0; e<boxArray.length; e++){
if(boxArray[e].scaleX > 2){
passDamage();
boxArray[e].die();

}
}
}


}
public function passDamage(){
trace('pass damage');
collisionOne=true;
if(collisionOne){
trace('collision One');
hbar.width -=5;

}

}