Space Shooter - Game Pause and Sound
| Category | : Flash | Views | : 58834 | ||
| Version | : 8 | Rating | : | ||
| Type | : Text | Source File | : space-shooter-pause-game-sound.zip | ||
| Result | : See the result | ||||
| <<< Previous | Index Series |
This tutorial is a series of tutorials that will teach you how to create Space Shooter Game. The tutorial is divided in 5 parts. If you haven't learned the previous tutorials, please see Space Shooter Flash Game
This is the last part of our game and we are going to add sound and a pause key.
Before we start with sound we need to make some changes. You may have noticed that our ship shoots bullets as much as the player press the space bar, that's not right as it makes the game much easier.
To prevent that, we are going to add a timer and the ship will shoot as much as WE want.
First add a new variable:
var timer = 8;
Now make these changes:
this.onEnterFrame = function()
{
timer++;
.
.
.
if (Key.isDown(Key.SPACE))
{
i++;
if(timer >= 8)
{
_root.attachMovie("Bullet", "Bullet" + i, _root.getNextHighestDepth());
_root["Bullet" + i]._x = Ship._x + 3;
_root["Bullet" + i]._y = Ship._y;
timer = 0;
}
}
}
The code runs like this:
1. If timer >= 8 then shoot a bullet and set timer to 0
2. Now that timer = 0, the bullet wont shoot, so the player has to wait all the code to run at least 8 timer to shoot again (timer++ will increase timer by 1 every frame)
3. Timer will be bigger than 8 again and the player can shoot.
Before:

After:

We can add the sound now. You need to have an mp3 file that's going to be the sound played when you fire. You can download one right here. Or simply download the source file that contains sound file.
Now that you have your sound, click File > Import > Import to library...
Select your mp3 file and click open, a new file should appear on the library. Right click it and click "Linkage..."
Check "Export to Actionscript..." and set the Identifier to "shoot".
This is the same thing we did previously with the Bullet Movie Clip, we need the linkage to call this on the code.
Add this to the shooting code:
if (Key.isDown(Key.SPACE))
{
i++;
if(timer >= 8)
{
_root.attachMovie("Bullet", "Bullet" + i, _root.getNextHighestDepth());
_root["Bullet" + i]._x = Ship._x + 3;
_root["Bullet" + i]._y = Ship._y;
var shoot_sound = new Sound();
shoot_sound.attachSound("shoot");
shoot_sound.start();
timer = 0;
}
}
![]() |
![]() |
![]() |
![]() |
![]() |
| <<< Previous | Index Series |







