Per your first problem, it does not appear i
increments - it's always 0
.
When you assign name, increment i
:
ast.name = "ast_" + (i++).toString();
Basically, saying i = i + 1;
Next up, hit test against the instance itself, not an identity:
ship.hitTestObject(ast)
Not sure how your game play works, but it would seem what you really want are two handlers:
- one to occasionally add a new asteroid
- one that tests for collisions
Currently your addAsteroid()
function adds a new asteroid and immediately tests if it collides with the ship upon creation. That asteroid will never be tested for collision again. If this is similar to a classic asteroids game, you may want to push each asteroid to an array, and add an event listener for ENTER_FRAME
to test each asteroid for collision against the ship.