0

I am trying to test the collisions between a bullet and an array of enemies in Actionscript 2. However it is not sensing a collision. This is the code in the bullet.

onClipEvent(load)
{
    facing = _root.player.facing;
    speed = 1;
    i = 0;
}

onClipEvent(enterFrame)
{

if (this._name != "bullet")
{
    this._x += facing * speed;

    while (i < _root.enemyID)
    {


        if (Math.abs(this._x - _root.enemies[i]._x)<10)
        {
            trace("hit enemy");

        }
        i++;
    }
}

}

1
  • I suggest moving right along and looking at ActionScript 3. I then recommend using an open source physics library like Box2D to do this for you. Commented Mar 8, 2012 at 0:49

1 Answer 1

1

The variable i looks like it is being set to 0 only on load. So it will be checking all the enemies on the first frame, but since i will now always be greater than enemyID, it will never go into the loop again.

Try setting i = 0; just before the while loop.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, that was the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.