Thursday, October 14, 2010

Create a shooting game using AS3: Part 1

Today we will start creating a shooting game in Flash.

This tutorial is OOP based, so if you don't know the basics of Object Oriented Programming, check out my previous tutorials.

This is the first part of the series, we will create ship movement with mouse and bullet spawning when mouse is held down.

Firstly, create 2 MovieClips - a space ship and a bullet. The paths to them should be cl.kirill.Ship and cl.kirill.Bullet respectively. Of course, those are just my names and my directories, you can change them to MyMegaClass.BrickShooterExtreme for all I care, just remember to change the name of packages too.

Now, remove the bullet MC from stage, but keep the ship. Move it a little to the bottom, on the level where it will be moving (horizontally).

Code for the

package cl.kirill
import flash.display.MovieClip;

public class Ship extends MovieClip
private var mouseIsDown:Boolean=false;
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{
private function mDown(MouseEvent):void{
private function mUp(MouseEvent):void{
private function shootBullets(Event):void{
var aBullet:Bullet = new Bullet();

As you can see, we have a listener for mouse to move our ship. You can simply assign the X (or Y, depends on you) of the ship to the X (Y) of mouse, however, I use (stage.mouseX-this.x)/5 to make the movement more smooth. You can change the "5" for more/less smoothness.

The next 2 listeners are for telling when are we holding our mouse, and the final listener adds bullets to the stage. Right now there is no interval between shots, but its just the first step - the base for our final game.

Here's the code:

package cl.kirill
import flash.display.MovieClip;

public class Bullet extends MovieClip
public function Bullet()
trace("Bullet fired!");

Yes, that's all that there is to it right now. We simply create new instances of Bullet when we hold our button, but there are no enemies to fire at, no powerups to collect and the bullets stay still... But we will continue next time!

Thanks for reading!


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


assassingames said...

Nice job.

electronic signature said...

Good to see such an interesting post.I always likes such a creative things Surely I am going to go through all parts of it from 1 to 8. But in this first I want to ask a question how to change the color of the bullet.I hope its easy but unable to locate the code

eznel said...

I have a problem on the bullets does not appeared the way your tutorial me..

Premium Accounts said...

thanks dear nice game and nice blog

Post a Comment