Followers 0

# Projectile hitTestObject bugg?

## 1 post in this topic

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 degrees:Number = Math.round((radians * 180 / Math.PI));

// Set movement and rotation.
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!

Edited by AmirOssanloo
0

##### Share on other sites

Is it possible to delete a post? I think the problem was simply that my break; is in the wrong place. I always break on the first loop, it should be inside the if-statement.

0

## Create an account

Register a new account