views:

64

answers:

2

So in my game, every 'enemy' movieclip creates a textfield 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?

+1  A: 

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.

Selene
I see what you're saying, but this doesn't really fix the problem. I am trying to prevent a collision detect with the textfield exclusively. With your method, if the laser passes through the textfield (which floats above the enemy mc) and hits the enemy, it won't count as a collision (which it should). In other words, I want textfield to be ignored completely by the collision check. Thanks anyway.
Miles
Have you tried defining a `hitArea` on your enemies, like my second suggestion? If your CollisionEngine works on Sprites or subclasses of Sprites, and you define a hitArea that doesn't include the enemy name, you should get the result you want.
Selene
A: 

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))
shortstick