Saturday, October 16, 2010

Create a shooting game using AS3: Part 3

Today we will cover these topics: making an interval between bullet shots, creating enemy manager, spawning and moving enemies.

Here's what we will get by the end of this tutorial - the enemies move, but don't interract with the player in any way... yet.


First of all, the shooting interval. We're going to use a setInterval method to call a function every period of time. In that function, we will check for mouseDown and create bullet if necessary.

My edited Ship.as:

package cl.kirill
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.*;

public class Ship extends MovieClip
{
private var mouseIsDown:Boolean = false;
public var myManager:BulletManager = new BulletManager();
private var bulletInerval = setInterval(createBullet, 400);

public function Ship()
{
trace("Ship added to stage!");
stage.addEventListener(Event.ENTER_FRAME, moveShip);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
}
private function moveShip(Event):void
{
this.x+=(stage.mouseX-this.x)/5;
}
private function mDown(MouseEvent):void
{
mouseIsDown = true;
}
private function mUp(MouseEvent):void
{
mouseIsDown = false;
}
private function createBullet():void
{
if (mouseIsDown)
{
var aBullet:Bullet = new Bullet();
stage.addChild(aBullet);
aBullet.x = this.x;
aBullet.y = this.y;
myManager.bulletArray.push(aBullet);
}
}
}
}

The edits are: added import of utils (for setInterval), created createBullet function instead of shootBullet, created a setInterval and linked it to the createBullet function.

Now I only shoot bullets every 0.4 seconds!

The next step - creating the enemies. First, draw your enemy (he must be facing the player, obviously) and convert him to MovieClip. Name its class as 'Enemy' and store in the library (remove from stage).

Here's my plan: create an enemy manager, which manages all the enemies. It keeps them all in an array and does actions through loops and methods. There is one problem though - how do we use the addChild method from an external class, to create something on stage? Here's my method - I pass the 'stage' property through the EnemyManager object as a paremeter. Then I pass the value to a variable called _stage and use _stage.addChild().

First, here is my Ship.as code (again) with more edits related to enemies:

package cl.kirill
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.utils.*;

public class Ship extends MovieClip
{
private var mouseIsDown:Boolean = false;
public var myManager:BulletManager = new BulletManager();
public var myEnemyManager:EnemyManager = new EnemyManager(stage);
private var bulletInerval = setInterval(createBullet, 400);

public function Ship()
{
trace("Ship added to stage!");
stage.addEventListener(Event.ENTER_FRAME, moveShip);
stage.addEventListener(MouseEvent.MOUSE_DOWN, mDown);
stage.addEventListener(MouseEvent.MOUSE_UP, mUp);
myEnemyManager.spawnEnemies(5);
}
private function moveShip(Event):void
{
this.x+=(stage.mouseX-this.x)/5;
}
private function mDown(MouseEvent):void
{
mouseIsDown = true;
}
private function mUp(MouseEvent):void
{
mouseIsDown = false;
}
private function createBullet():void
{
if (mouseIsDown)
{
var aBullet:Bullet = new Bullet();
stage.addChild(aBullet);
aBullet.x = this.x;
aBullet.y = this.y;
myManager.bulletArray.push(aBullet);
}
}
}
}

You can see that I added an EnemyManager object, passed stage through it and used the spawnEnemies method of my Manager. Now, the Manager itself (EnemyManager.as):

package cl.kirill
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;

public class EnemyManager extends MovieClip
{
public var enemyArray:Array = [];
private var _stage;

public function EnemyManager(get_stage:Stage)
{
trace("Enemy manager added");
this.addEventListener(Event.ENTER_FRAME, managing);
_stage = get_stage;
}
public function spawnEnemies(num:Number):void
{
for (var i:int=0; i<num; i++)
{
var anEnemy:MovieClip = new Enemy();
enemyArray.push(anEnemy);
_stage.addChild(anEnemy);
anEnemy.x +=  30 + i * 80;
trace("Enemy spawned: " + anEnemy.height);
}
}
private function managing(Event):void
{
for (var u:int=0; u<enemyArray.length; u++)
{
enemyArray[u].y +=  5;
if (enemyArray[u].y > 400)
{
enemyArray[u].y = 0;
}
}
}
}
}

I simply spawn the enemies and add them to the array. I have a loop that checks the array and moves the MovieClips. This is just simple movement - no attacks, no deaths yet.

Thanks for reading!

Related:

Create a shooting game using AS3: Part 1
Create a shooting game using AS3: Part 2
Create a shooting game using AS3: Part 4
Create a shooting game using AS3: Part 5
Create a shooting game using AS3: Part 6
Create a shooting game using AS3: Part 7
Create a shooting game using AS3: Part 8

No comments:

Post a Comment