So in my game, every 'enemy' movieclip creates a t开发者_Python百科extfield that represents the name of the enemy. The problem is, I want my collision detect engine to detect a collision with the enemy itself, not the textfield.
This is the code that I have currently have on my collision class for detecting a collision with the enemy:
for(var i = 0;i < _enemies.length; i++)
{
if(CollisionEngine.isColliding(_laser, _enemies[i]._enemyNameTextField, _animation))
{
}
else if(CollisionEngine.isColliding(_laser, _enemies[i], _animation))
{
_enemies[i].kill();
_animation._killedEnemy = true;
}
}
The first if clause checks for a collision with the enemy's text field. The else if checks for a collision with the enemy.
The problem with this current implementation is that if the 'laser' hits the textfield first, passes through it, and hits the enemy, it's not detected as a collision.
Any idea on how I could work around this?
Change the order of the checks:
if(CollisionEngine.isColliding(_laser, _enemies[i], _animation))
{
if(!CollisionEngine.isColliding(_laser, _enemies[i]._enemyNameTextField, _animation))
{
_enemies[i].kill();
_animation._killedEnemy = true;
}
}
Note that the above code is equivalent to:
if(CollisionEngine.isColliding(_laser, _enemies[i], _animation) && !CollisionEngine.isColliding(_laser, _enemies[i]._enemyNameTextField, _animation))
{
_enemies[i].kill();
_animation._killedEnemy = true;
}
Alternatively, you can explicitly define a hitArea
on all of your enemies so that collisions with the text field aren't considered collisions.
in your enemy have a method like:
public function getCollision():Sprite{
//Where this.body is the main animation for the clip/Hittest area
return this.body
}
or something that returns just the active hit area, then you can run:
if(CollisionEngine.isColliding(_laser, _enemies[i].getCollision(), _animation))
精彩评论