I have a basic space shooter game made in AS3.
The game currently consists out of one enemy and the player - a movable turret.
The premise of the game is, when the enemy is shot, it respawns, and the player score goes up with one point.
Now I want to extend the game by having more than one enemy on the playing field. The amount of enemies can differ for each wave, so I do not really want to keep track of them individually.
When I want to create an enemy, I call (from within my main class)
this.addChild(createNewEnemy());
with the createNewEnemy() function creating, and returning an anonymous Enemy object.
My question is, how do I perform hit tests on these anonymous Enemies with the bullet being fired by the player? When an enemy collides with a bullet, it should be removed and the score should be incremented.
I'm not quite sure what you mean by "anonymous object." You really ought to be creating these things in such a way that can track them. One way would be perhaps override your addChild method for the container sprite and push new enemies to a vector. Example:
var enemies:Vector.<Enemy> = new Vector.<Enemy>();
override public function addChild(child:DisplayObject):DisplayObject
{
if(child is Enemy) {
enemies.push(child);
}
super.addChild(child);
}
Now you can simply test against the enemies vector, or use the same "is
" keyword in your bullet collision to check and see if the colliding display object is of based type Enemy
.
As far as the actual code to do the collision detection, see the following answer: https://stackoverflow.com/a/7083965/562566