6: Adding Sound Effects

This tutorial builds on Part 5 of the six-part tutorial series on building a Space Shooter and you will use some of the same techniques as used there.

We will add a sound when the player fires their weapons.

Prerequisites: Please complete Tutorial 5 first.

Tools & Techniques: incorporating sounds, increasing difficulty This tutorial requires you to use some of the same techniques adopted in previous sections.

We’re going to need some sound effects for this. I’ve used a free sound effect called 'comedy_missile_launch' from: http://www.freesfx.co.uk.

Note sounds will overlay each other, so short sounds are best (roughly 1 second).

You may need to use a sound editor to change file types from mp3 to wav if you haven’t the relevant plugins. Packages like Adobe Audition are great for this but there are free solutions available on the Internet such as Audacity.

In Adobe Animate, select ‘Import’ and ‘Import to Library’ to begin to load your new sound effect into your application.

Then simply select your sound file and click 'Open'.

The sound file will now appear in the library, but to use it in our code we will need to export to ActionScript. Right-click on the sound file in the library, and select properties:

You will be brought to an 'Options' tab, navigate to the 'ActionScript' tab instead.

Select the 'Export for ActionScript' checkbox (the next checkbox will become checked automatically).

You may like to change the Class name to something more suitable (and using better spelling), such as 'MissileSound'.

Click OK.

You may get the following warning, simply click OK.

The sound file in the library now has the class name in the Linkage column, so we can use this to create an instance of the sound in our code.

We’re now ready to insert the code to allow this sound file to play whenever we fire our ship’s weapon.

Insert this new instantiation statement with the global declarations at the top of your code. Its exact position isn't too important, but you may like to place it after the lines:


var score: int = 0;
var lives: int = 3;


var shoot_sound:Sound = new MissileSound();

We will add the following code to play the sound when bullets are fired.

Locate the IF statement that calls 'addBullet()' when the spacebar is pressed and the cooldown timer is less than or equal to zero, and add the code within the braces after the other statements:


if (spaceBar && cooldown <= 0)
    addBullet(player_mc.x, player_mc.y);
    cooldown = 8;



Test your application and good luck. Space Game 6 And

Test your application, and good luck!


There we have it, you now have the basics to your first space shooter, side scrolling, game.

There may be some polishing needed, more sounds, backgrounds, more varied enemies, etc. Perhaps some power-ups or addition weapons will add interest for the player. You may like to randomise when the enemies come in, or increase their number when each is killed.

Think through what challenges you would like to include and how you might go about implementing them

Code can, and should, be challenging if it is to be rewarding. The initial difficulty in finding solutions to problems turns many away, but if you do push through it there can be a great sense of accomplishment in the products you create.

Share your experiences, the hurdles you overcame, and post your solutions online to inspire others.

The world of code awaits.