Skip to main content
April 30, 2014
Question

Help!! Setting "body" for enemy in shooter!!

  • April 30, 2014
  • 1 reply
  • 582 views

I tried some code for my enemyShooter's hitTest and it worked.

if (_root.enemyShooter.body.hitTest(_root.ship.body)) {...}

(enemyShooter is the instance name of movieclip)

However, this doesn't work.

var name = _name

if (_root.name.body.hitTest(_root.ship.body)) {...}

The reason I am doing this is because there are many enemies like enemyShip1, enemyShip2, enemyShip3.

The name if it is set by

"EnemyShip" + _root.getNextHighestDepth()

because I use

_root.attachMovie("EnemyShip", "EnemyShip" + _root.getNextHighestDepth(), _root.getNextHighestDepth());

I cannot find the instance name unless using _name

How can I make it work?

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
April 30, 2014

in most situations it's best to store those enemy references in an array and loop through the array especially if you're sometimes removing enemies and they no longer need to be checked for a hittest (or anything else).

May 1, 2014

Yes, I already used the Array feature.

My whole code:

class EnemyBasic extends MovieClip

{

    var _x, _y, speed, removeMovieClip, hitTest, health, shaking, shakeDuration, _rotation, name, _name;

    function EnemyBasic()

    {

        super();

    } // End of the function

    function onLoad()

    {

        _x = 700;

        _y = Math.random() * 200 + 50;

        speed = Math.random() * 3 + 3;

        _root.ship.enemies.push(this);

                    health = 2;

                    shaking = false;

        shakeDuration = 10;

        _rotation = 0;

                                        name = _name;

    } // End of the function

    function onEnterFrame()

    {

        _x = _x - speed;

        if (_x < -100)

        {

            this.removeMovieClip();

        } // end if

                    trace(name)

        if (_root.ship.enemies.hitTest(_root.ship.body))

        {

            this.explode();

        }

    }

} // End of Class

Some of the code are removed to make this simpler.

How do I change the HitTest part? _root.ship.enemies doesn't work

kglad
Community Expert
Community Expert
May 1, 2014

why doesn't _root.ship.enemies work?

is it populated when you start your hittest?  if yes, how are you trying to use it?