Space Shooter - Game Pause and Sound

Category : Flash Views : 25229
Version : 8 Rating
3.4/5 (52 votes)
Type : Text Source File: space-shooter-pause-game-sound.zip
Result: See the result


<<< PreviousIndex Series

Previous | 1 | 2 | Next

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!


del.icio.us digg it Reddit Stumble Upon Technorati

Previous | 1 | 2 | Next

<<< PreviousIndex Series




Supporters

Reseller Web Hosting