Hi,
Actionscript 3 problem incoming!
I have some problems with my projectiles. It seems like my hitTestObject sometimes fires more then once... I don't understand why since I destroy the projectile once it has reached it's target.
The projectile state is updated in the gameloop once it has been created. And is removed (from the vector) once it is destroyed.
/**
* Update projectile state.
*/
public function update():void
{
// If projectile do not have a target.
if (target == null)
{
destroy();
return;
}
// Calculate movement and rotation.
var dx:Number = target.x - x;
var dy:Number = target.y - y;
var radians:Number = Math.atan2(dy, dx);
var degrees:Number = Math.round((radians * 180 / Math.PI));
// Set movement and rotation.
this.x += Math.cos(radians) * _speed;
this.y += Math.sin(radians) * _speed;
this.rotation = degrees;
// Check collosion towards target.
if (this.hitTestObject(target))
{
target.onHit(this);
destroy();
}
}
/**
* Destroy projectile.
*/
public function destroy():void
{
for (var i:int = 0; i < _projectileManager.projectiles.length; i++)
{
if (_projectileManager.projectiles[i] == this)
{
_projectileManager.projectiles.splice(i, 1);
this.parent.removeChild(this);
target = null;
}
break;
}
}
If I put a trace inside the if collision statement I can see that it is fired a lof of times (sometimes)... How can this not be reliable? If it checks for collision and then is destroyed once the collision has occured - then why would the if statement go trough more then once.
Thanks!
Edit: Topic should be with one g in bugg!