Sign in to follow this  

Projectile hitTestObject bugg?

This topic is 1555 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi,

 

Actionscript 3 problem incoming! smile.png

 

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!

Edited by AmirOssanloo

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this