Developing a space shooter game
[ October 07, 2004 ] by Richard Nias aka Crashlanding
In this collection of tutorials, the author explains how to create a basic shooter game, considering all its different aspects: movement, shooting, enemies, sound, etc.


SHOOTING

In the first tutorial we looked at movement, possibly the most important thing in most games. Now we will look at shooting, which seems quite complex at first, but is really simple. So lets look at what we will be making (spacebar to fire).

At the moment bullets fly out constantly, but in a later tutorial we will limit this.

Start off by drawing your bullet. Go to Insert/New Symbol and click on "Advanced" if that section is not there already. call it bullet and click the "Export for ActionScript" checkbox.

It can be anything, but I decided to keep it simple and just use a yellow dot. Make sure the bullet if positioned relative to the registration point like this:

When you've done that, go back to the main stage open the ActionScript pane and at the end of the moveHero function put this:

if (Key.isDown(Key.SPACE))
{
  fireBullets();
}

We covered this sort of thing last time. When the spacebar is pressed, the function fireBullets is called. Now for the function:

var i;
 
function fireBullets()
{
  i++;
 
  var newname = "bullet" + i;
 
  _root.attachMovie("bullet", newname, i*100);
  _root[newname]._y = _root.hero._y + 13;
  _root[newname]._x = _root.hero._x + 55;
 
  _root[newname].onEnterFrame = function()
  {
    var bullet_speed = 7;
 
    this._x += bullet_speed;
 
    if (this._x > 555)
    {
      this.removeMovieClip();
    }
  }
}

Before the function even starts the variable "i" is set without a value and each time the function is called this variable increases by 1. A name "bullet"+i is made and then we come to the attachMovie function. This attaches a movieclip from the library and gives it a new name. "bullet" tells flash which Movieclip to attach, the next parameter is the name of the new movieclip and the last parameter is Depth, which you don't need to worry about for this sort of game. Next, the new movieclip is put to the correct x and y relative to the hero (that's what the "+13" etc. is for: adjust those values to fit the size of your hero).

Now to make the bullet move. An OnEnterFrame function for the new bullet is called. "bullet_speed" is the speed of your bullet, which is added to the _x value of the bullet to make it move. And finally, if the bullet goes of the screen to save speed the bullet is removed with removeMovieClip.

That's it! In the next tutorial we'll look at enemies. If you have any problems e-mail me at richard@livescripts.net.
You can download the source here.


     
 
 
Name: Richard Nias aka Crashlanding
Location: South-east London, UK
Age: 14
Flash experience: Had Flash for 1 1/4 years now. I learnt from the help files before finding gotoandplay and flashkit, getting me intrested in games.
Job: None
Website: http://flash.livescripts.net
 
 
| Homepage | News | Games | Articles | Multiplayer Central | Reviews | Spotlight | Forums | Info | Links | Contact us | Advertise | Credits |

| www.smartfoxserver.com | www.gotoandplay.biz | www.openspace-engine.com |

gotoAndPlay() v 3.0.0 -- (c)2003-2008 gotoAndPlay() Team -- P.IVA 03121770048