Our Flash tutorials provide you with step-by-step instructions on how to create beautiful Flash animations and integrate them into your website.  Home Flash & Swish Flash Tutorials Space Shooter - Movie Clip and Movement

Space Shooter - Movie Clip and Movement

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: Webzo Studio
Browse Pages: << < 1  2  3  4  5 
Final results of our readers
New!
Passed through all the steps? Share your result!
Your result will be premoderated.
Please make sure you choose the right image.
 
 



Captcha

*Required fileds
Our Flash tutorials provide you with step-by-step instructions on how to create beautiful Flash animations and integrate them into your website. More Flash Tutorials: Featured Materials | Fresh Materials | More Flash Tutorials at FlashPerfection.com

Reader's comments
comments Joey December 07, 2011 says:
I DONT WORK. Could you please find your error in the coding?
Reply
comments MaryA2900 April 13, 2011 says:
Hi, I am confused on what replaces the word 'this' or if it stays the same.
Reply
comments Tammie October 14, 2010 says:
I can't get this page to work either. It is very confusing of where to paste the code.
Reply
comments Tommy March 24, 2010 says:
hey i got to the of this page and i cant go any further because i keep on recieving this error when i try to test my movie
it looks like this Operator '=' must be followed by an operand now this happened when i pasted the actionscript for the bullets
can you help me out?

Reply
Add comments to "Space Shooter - Movie Clip and Movement"

Captcha