Jump to content

  • Log In with Google      Sign In   
  • Create Account


Projectile hitTestObject bugg?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 AmirOssanloo   Members   -  Reputation: 128

Like
0Likes
Like

Posted 14 October 2013 - 01:12 PM

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, 14 October 2013 - 01:22 PM.


Sponsor:

#2 AmirOssanloo   Members   -  Reputation: 128

Like
0Likes
Like

Posted 14 October 2013 - 02:15 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS