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.


SOUNDS

Sounds are very important for games, as they help the player concentrate more on the game, and they are also useful for emphasizing certain events in the gameplay. By the the end of this tutorial, you should have something like this:

First you need to get your sounds. I got my effects and loop from FlashKit for free, but you can make your own or get them from a different source. It doesn't matter how long the sounds are, and bear in mind it is best to make sure your music loops seamlessley. Once you have your sounds, export them into flash, open the library and on your music loop right click / linkage and select export it to actionscript with the name "music". Repeat this with your laser and explosion sounds, naming them "laser" and "explode" respectively. Now your sounds are ready, make a "Music On/Off" button, looking something like this:

Now for the coding. First we need to get our sounds into the script with this code:

var laser = new Sound();
laser.attachSound("laser");
var explode = new Sound();
explode.attachSound("explode");
var music = new Sound();
music.attachSound("musicloop");
music.start();

This code creates sound objects with new Sound(); and imports the sounds into actionscript with attachSound();, and starts the music sound using start(); Now we need to make the music loop. To do this, we could use start() like this:

start(secondsoffset, loop);

We would leave the first parameter as 0 and loop as the number of ties you want the sound to loop. Unfortunatly, this can't be an infinite number. To make the music loop forever, add this code to the end of your script:

music.onSoundComplete = function()
{
  music.start();
}

This checks wether the sound is complete, and if it is, it starts it again. The only problem with this is that it can slow the game down slightly when the music ends, but that shouldn't matter too much. Remember: you don't need to put this in an onEnterFrame function, as it checks every frame automatically. Now, in the shooting function you need to add:

laser.start();

This, obviously, starts the laser sound whenever you shoot. Next, find the bullet and enemy hitTest and add this:

explode.start();

This also needs to be added to the hitTest in the enemies script.


RESETTING THE SOUNDS

At the moment you'll find that if you die, and then you press restart the music starts playing again, while it is still on. To stop this from happening, we need to stop the original music when you restart. Change the restart button's actions:

on(release)
{
  stopAllSounds();
  gotoAndStop(1);
}

This resets every thing for your next game.


THE ON/OFF BUTTON

Sometimes people want to play a game and not hear all the sounds. In these cases, it is always good to have a button to turn the sound off. We've made the button already, now we need to program it.

var sound_on = true;

Add this to the top of the script. This tells you wether the sounds are toggled on or off. Now, add this to the onEnterFrame function:

if(sound_on != true)
{
  stopAllSounds();
}

So, if the sound_on variable equals false, every frame stopAllSounds is called, cancelling out any explosions, etc. Now add to the button's actions this code:

on (release)
{
  if (_root.sound_on == true)
  {
    _root.sound_on = false;
  }
  else
  {
    _root.sound_on = true;
    music.start();
  }
}

Every time the button is released, first sound_on is checked. If it equals true, it is changed to false and all the sounds are stopped. If it equals false, it is changed to true and the music is started again.

Well done! You have completed the sounds tutorial! If you have any problems don't hesitate to ask at richard@livescripts.net. You can download the fla. 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