Aeroplane Shooting Game Tutorial – Adding Enemies

aeroplane-game-tutorial3

As of now we have a player plane which can be controlled using the keyboard. The player plane can shoot bullets so we need now some enemies to dodge and shoot at. We are going to add the enemies in the same way we dealt with the player bullets. We are going to use a Vector which stores the enemy planes. We add 2 members to our main class: the vector storing the enemies and a variable to define the enemy speed, so we don’t hardcode it.

private var ENEMY_SPEED:Number = 3;
		
private var enemyPlanes:Vector.<MovieClip> = new Vector.<MovieClip>();

Unlike the player bullets the enemies creation is slightly different. We create the enemies in the top part of the screen at random x positions and at random times. We add a new method which creates the enemies:

private function createEnemy():void
{
	var enemy:MovieClip = new EnemyRes();

	enemy.x = enemy.width + Math.random() * (440 - enemy.width);
	enemy.y = -enemy.width/2;

	this.addChild(enemy);

	enemyPlanes.push(enemy);
}

At this moment we have the function which creates enemies but we have to trigger it(at random times). In enterFrame method we add the code to create enemies at random times, the code to update enemy planes positions and to destroy the enemies which went out of the screen boundaries:

private function enterFrame(e:Event):void
{
	...

	// add enemy planes
	if (Math.random() < 0.01)
	{
		createEnemy();
	}
			
	// update enemies
	for each( var enemy:MovieClip in enemyPlanes)
	{
		enemy.y += ENEMY_SPEED / 2;
	}
			
	// remove enemies which are getting out of the screen
	while(enemyPlanes.length > 0 && enemyPlanes[0].y > 480 + 32)
	{
		this.removeChild(enemyPlanes.shift());
	}
}

Now that we have enemies we need to add them the functionality to shoot bullets. First of all we create a new method to create a bullet for a specific plane.

private var ENEMY_BULLET_SPEED:Number = 4;
		
private var enemyBullets:Vector.<MovieClip> = new Vector.<MovieClip>();


private function createEnemyBullet(enemy:MovieClip):void
{
	var enemyBullet:MovieClip = new BulletRes();
		
	enemyBullet.x = enemy.x;
	enemyBullet.y = enemy.y + enemy.width / 2;
	enemyBullet.rotation = 180;
			
	this.addChild(enemyBullet);
			
	enemyBullets.push(enemyBullet);
}

Then we need to invoke the function as we did for the enemy planes. We add the following code in enterFrame method to deal with the enemy bullets(create, update positions and destroy the bullets getting out of the screen boundaries).

private function enterFrame(e:Event):void
{
	...

	// shoot enemy bullets
	if (Math.random() < 0.02 && enemyPlanes.length > 0)
	{
		enemy = enemyPlanes[Math.floor(Math.random() * enemyPlanes.length)];
				
		createEnemyBullet(enemy);
	}
			
	// update enemies
	for each( var enemyBullet:MovieClip in enemyBullets)
	{
		enemyBullet.y += ENEMY_BULLET_SPEED;
	}
			
	// remove enemies which are getting out of the screen
	while(enemyBullets.length > 0 && enemyBullets[0].y > 480 + 32)
	{
		this.removeChild(enemyBullets.shift());
	}
}

Now the output of this tutorial looks more like a game. We have a player plane shooting bulets, enemies planes shooting bullets too, coming from the top. We didn’t deal yet with collisions, this will be handled in the next part of this tutorial.

Browse the code for this part here.

One thought on “Aeroplane Shooting Game Tutorial – Adding Enemies

Leave a Reply

Your email address will not be published. Required fields are marked *