website promotion banner
eturnkeys
Your Ad Here
Flash & Swish  Home Flash & Swish Flash Tutorials Space Shooter - Game Pause and Sound
rss

Space Shooter - Game Pause and Sound

Author: Webzo Studio More by this author
Browse Pages: << <  1  2  3  4  5


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:

image 1

After:

image 2

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 MC, 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; } }

The first thing we do is declare a new Sound():

var shoot_sound = new Sound();

Then we attach the sound we have on our library to this variable:

shoot_sound.attachSound("shoot");

And we tell our sound to play using the start() method:

shoot_sound.start();

Run the code (Ctrl + Enter) and see the results.

Now we are going to add a pause to our game.

Add a new variable to know whether we are paused or not:

var gpause = false;

Then make some changes to ALL codes.

Main code:

this.onEnterFrame = function() { if (!gpause) { //this is the rest of the code we did previously. } }

Enemy's code:

onClipEvent(enterFrame) { if (!_root.gpause) { this._x -= mySpeed; if (this._x < -10) { reset(); } } }

Bullet's code:

this.onEnterFrame = function() { if (!_root.gpause) { //rest of the code we did } }

It's pretty simple; if gpause is false the code will run normally, but if gpause is true the code will not run.

Add another code to the main timeline:

this.onEnterFrame = function() { if (!_root.gpause) { //rest of the code we did } if (Key.isDown(Key.CONTROL)) { if (gpause) { gpause = false; } else if (!gpause) { gpause = true; } } }

If the players press "ctrl" we set gpause to true or false depending on the value of gpause.

This needs to be outside "if (!_root.gpause)" because we need to check if the user pressed the pause key even if the game is paused.

The game is done! I hope you enjoyed developing it. And see you in the next series!



About the Author:

Webzo Studio is a web design company located in the United States. Webzo Studio was founded in August 2005. We are a small team of designers that just love our job. The love of doing what we do is what makes us not doing our job as a job, but as an enjoyment.
read more about this author


Author's URL: www.webzo.org

print this page tell a friend subscribe to newsletter subscribe to rss
Rate this Material: Bad 1 2 3 4 5 Excellent
Browse Pages: << <  1  2  3  4  5

Add comments to "Space Shooter - Game Pause and Sound"