Flash Game - Part 5

Bullet holes and ricochet sound effects:

I will go through the bullet holes effect, the ricochet effects, and change the way the sound is made for the gun shot.

The layers should look something like this. Don't worry if you have more, just add the one that isn't there - actions:

Make a new movie clip called "hole". Draw a bullet hole. Don't drag it on to the stage. Right click on it in the library and go to "Linkage":

Then click on Export for ActionScript and give it the Identifier "hole" then click OK:

You should have two sounds, for the ricochet and the gun shot. The part 2 source files contain a gun sound effect already in the library - you'll need to import one for the ricochet - there are plenty on the 'net. Once you have imported it, follow the same procedure as above. Right click on the sounds, go to Linkage, then Export for ActionScript and give them Identifiers. In this example I use "rico" and "gun".

Click in the first frame in the actions layer. Add the following actions:

stop();
ricochet = new Sound();
ricochet.attachSound("rico");
gunshot = new Sound();
gunshot.attachSound("gun");

Explanation:

ricochet = new Sound();
Defining a new sound object.

ricochet.attachSound("rico");
Attaching the sound exported with the identifier"rico" as ricochet.

The same principle applies for the gun shot.

Click on the sights movie clip. Press F9 for to bring up the actions window. Use the following actions - the addition is in bold:

onClipEvent (load) {
Mouse.hide();
this.swapDepths(9999);
}

Only the onClipEvent (load) code needs to be modified.

this.swapDepths(9999); makes the sights movie clip the highest on the instance stack, ie. it will appear on top of everything under the depth of 9999. Now this is because when you shoot the gun, bullet holes will appear on the wall, and you don't want the bullet holes appearing on top of the sights. It's unlikely someone will sit there making 9999 bullet holes, but it's a high number - out of the way.

Now you should have the linkage for the hole movie clip and the two sound effects.

Double click on the gun movie clip or right click and go to Edit. Click on the second frame and change the sound properties of the frame to "none". Return to the main time line, click on the the gun movie clip and press F9 to bring up the actions window - add the actions in bold:

onClipEvent (mouseDown) {
_root.gunshot.start();
play();
}

You only need to modify the onClipEvent (mouseDown) actions.

_root.gunshot.start(); starts the gunshot sound you defined in actions.

Now click on the ground. Press F8 and convert it into a movie clip. Put these actions on it:

on (press) {
_root.ricochet.start();
}

This starts the ricochet sound when the ground is clicked, or shot.

Click on the wall. Press F8 and convert it into a movie clip. Put these actions on it:

onClipEvent (load) {
c = 1;
}
on (press) {
_root.ricochet.start();
_root.attachMovie("hole", "h"+c, c);
_root["h"+c]._x = _root.sights._x;
_root["h"+c]._y = _root.sights._y;
c++;
}

Explanation:

onClipEvent (load) {
c = 1;

Sets the count to 1. This count determines the unique name of each bullet hole and depth.

on (press) {
_root.ricochet.start();

When you click, or shoot the wall, the ricochet sound starts.

_root.attachMovie("hole", "h"+c, c);
This attaches the hole movie, from the library, to the main time line. The "hole" is the identifier, "h"+c is the new name, so in the first case, it will be "h1" and c is the new depth, so again, in the first case, it will be 1. As you keep shooting the new names will be h2, h3, h4 etc and the depths will be 2, 3, 4, etc.

_root["h"+c]._x = _root.sights._x;
This sets the x of the bullet holes to the same as the x of the sights - to appear as if the hole appeared where you last shot.

_root["h"+c]._y = _root.sights._y;
This sets the y of the bullet holes to the same as the y of the sights - to appear as if the hole appeared where you last shot.

c++;
This increases the count so the next bullet hole will have a unique name and depth.

That covers the bullet holes, ricochet sound effects and the different method for producing the gun shot sound effect. One more bit of advice - you may want to edit the sights, wall and ground movie clips. They all should only have one frame in their time lines - it's good practice to put stops here, as the movie clips don't need to playing. It's negligible here, but in larger movies, everything adds up.

When you publish your movie, the sound settings determine the quality of your sound, and the size of your file. Here are some guide lines that may help when using sound in your Flash movies (taken from Flash help):

Using the ADPCM compression option

The ADPCM compression option sets compression for 8-bit or 16-bit sound data. Use the ADPCM setting when you are exporting short event sounds such as button clicks.

To use ADPCM compression:

1 In the Sound Properties dialog box, choose ADPCM from the Compression menu.

2 For Preprocessing, select Convert Stereo to Mono to convert mixed stereo sounds to mono (monaural). (Mono sounds are unaffected by this option.)

3 For Sample Rate, select an option to control sound fidelity and file size. Lower rates decrease file size but can also degrade sound quality. Rate options are as follows:

5 kHz is barely acceptable for speech.

11 kHz is the lowest recommended quality for a short segment of music and is one-quarter of the standard CD rate.

22 kHz is a popular choice for Web playback and is half the standard CD rate.

44 kHz is the standard CD audio rate.

Note: Flash cannot increase the kHz rate of an imported sound above the rate at which it was imported.

Using the MP3 compression option

The MP3 compression option lets you export sounds with MP3 compression. Use MP3 when you are exporting longer stream sounds such as music sound tracks.

If you are exporting a file that was imported in MP3 format, you can choose to export the file using the same settings the file had on import.

To export an imported MP3 file with the same settings the file had on import:

1 In the Sound Properties dialog box, choose MP3 from the Compression menu.

2 Select Use Imported MP3 Quality (the default setting). Deselect this option to choose other MP3 compression settings, as defined in the procedure below.

To use MP3 compression:

1 In the Sound Properties dialog box, choose MP3 from the Compression menu.

2 Deselect Use Imported MP3 Quality (the default setting).

3 For Bit Rate, select an option to determine the bits per second in the exported sound file. Flash supports 8 Kbps through 160 Kbps CBR (constant bit rate). When you are exporting music, set the bit rate to 16 Kbps or higher for the best results.

4 For Preprocessing, select Convert Stereo to Mono to convert mixed stereo sounds to mono (monaural). (Mono sounds are unaffected by this option.)

Note: The Preprocessing option is available only if you select a bit rate of 20 Kbps or higher.

5 For Quality, select an option to determine the compression speed and sound quality:

Fast yields faster compression but lower sound quality.

Medium yields somewhat slower compression but higher sound quality.

Best yields the slowest compression and the highest sound quality.

Using the Raw compression option

The Raw compression option exports sounds with no sound compression.

To use raw compression:

1 In the Sound Properties dialog box, choose Raw from the Compression menu.

2 For Preprocessing, select Convert Stereo to Mono to convert mixed stereo sounds to mono (monaural). (Mono sounds are unaffected by this option.)

3 For Sample Rate, select an option to control sound fidelity and file size. Lower rates decrease file size but can also degrade sound quality. Rate options are as follows:

5 kHz is barely acceptable for speech.

11 kHz is the lowest recommended quality for a short segment of music and is one-quarter of the standard CD rate.

22 kHz is a popular choice for Web playback and is half the standard CD rate.

44 kHz is the standard CD audio rate.

Note: Flash cannot increase the kHz rate of an imported sound above the rate at which it was imported.

Using the Speech compression option

The speech compression option exports sounds using a compression specially adapted to speech.

To use speech compression:

1 In the Sound Properties dialog box, choose Speech from the Compression menu.

2 For Sample Rate, select an option to control sound fidelity and file size. A lower rate decreases file size but can also degrade sound quality. Choose from the following options:

5 kHz is acceptable for speech.

11 kHz is recommended for speech.

22 kHz is acceptable for most types of music on the Web.

44 kHz is the standard CD audio rate. However, since compression is applied, the sound will not be of CD quality in the Flash movie.

Guidelines for exporting sound in Flash movies

Besides sampling rate and compression, there are several ways to use sound efficiently in a movie and keep file size down:

Set the in and out points to prevent silent areas from being stored in the Flash file and to reduce the size of the sound.

Get more out of the same sounds by applying different effects for sounds (such as volume envelopes, looping, and in/out points) at different key frames. You can get a number of sound effects using only one sound file.

Loop short sounds for background music.

Do not set streaming sound to loop.

When exporting audio in embedded video clips, keep in mind that the audio is exported using the global streaming settings selected in the Publish Settings dialog box.

Use stream synchronization to keep the animation synchronized to your sound track when you preview your animation in the editor. If your computer is not fast enough to draw the animation frames so that they keep up with your sound track, Flash skips frames.

When exporting QuickTime movies, use as many sounds and channels as you want without worrying about file size. The sounds are combined into a single sound track when you export as a QuickTime file. The number of sounds you use has no effect on the final file size.