Friday, October 15, 2010

Create a shooting game using AS3: Part 2

Today we let our spaceship shoot bullets that remove themselves when they go offscreen.

First of all, I want to say that there, of course, are a lot of ways of creating a shooting game. This is my method, I'm not saying it is the best one, but it works and is easy to understand/edit/manipulate in any way. The bullets I'm going to create will all be stored in an Array of a BulletManager class and all the bullet related stuff will be done through that class.

This means that we won't ever need to use the Bullet class. So, change the class URL of the bullet MovieClip in your library from cl.kirill.Bullet to simply Bullet.

Here is my current Ship.as:

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

public class Ship extends MovieClip
{
private var mouseIsDown:Boolean = false;
public var myManager:BulletManager = new BulletManager();

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);
stage.addEventListener(Event.ENTER_FRAME, shootBullets);
}
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 shootBullets(Event):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 notice a few differences between this code and the code from previous tutorials, these are the

public var myManager:BulletManager = new BulletManager();

and

myManager.bulletArray.push(aBullet);

lines. First I create an instance of the BulletManager class (which I will create later) and when I create a Bullet, I put it in the bulletArray array of that class.

Here is my BulletManager.as:

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

public class BulletManager extends MovieClip
{
public var bulletArray:Array = [];

public function BulletManager()
{
trace("Manager added");
this.addEventListener(Event.ENTER_FRAME, managing);
}

private function managing(Event):void
{
for (var i:int=0; i<bulletArray.length; i++)
{
bulletArray[i].y -=  10;
if (bulletArray[i].y < -10)
{
bulletArray[i].parent.removeChild(bulletArray[i]);
bulletArray.splice(i,1);
}
}
}
}
}

As you can see, I use a simple for loop to check each bullet, move it and remove if its off screen (removing the bullet is very important). Pretty simple, but effective.

Results:


Thanks for reading!

Related:

Create a shooting game using AS3: Part 1
Create a shooting game using AS3: Part 3
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