- Home
- Tutorials
- Flash
- Intermediate
- Sounds and preloaders
Sounds and preloaders

page1
Javier Cardona
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org.
View all articles by Javier CardonaDownload FLA | example
In this tutorial you will learn to make a very simple preloader and one way to insert sounds for use in your action script.
Ok, let's start. Open a new movie, and in the first frame draw a rectangle and name your only layer ?preloader?. Use a border and a fill. Choose any colors you want as long as they're different from the movie's background.
Select the fill and make it a symbol (press F8 or go to Modify | Convert to symbol). Name it loadBar and make sure the registration point is set to the left middle and that you're making a movie clip.

Then, with the new symbol selected, go to the property panel and give the instance the name loadBar.
Now, go to the actions panel and insert this code for loadBar:
onClipEvent (enterFrame) {
this._xscale = _root.getBytesLoaded()/_root.getBytesTotal()*100;
}
This tells loadBar that it should be 0% wide at the beginning and increase in horizontal size as the movie loads. That's as much of a preloader that we need to insert the sounds.
Now that we have a working preloader, create now a folder beneath the only layer. This folder will make it easier to have all your sounds together without taking to much space in the timeline. Insert a layer in this folder.Also, insert one frame in the preloader layer (press F5 or go Insert | Timeline | Frame).
Now we add the sound. Import your sound to the library. With the sound in the library, right click on it and select ?linkage?. In the dialog box, check the box Labeled ?Export for ActionScript? and make sure ?Export in first frame? is not selected (that is what causes the sounds to load before the preloader). In the ?Identifier? box type ?music?, that is the name by which we refer to the sound in ActionScript.

Select the first frame it the layer inside the folder in the timeline. In the property panel, select the name of your sound from the ?Sound? combo box. Make sure ?Synch? is set to ?Event?.

Select the same frame again and go to the actions panel. And insert:
stopAllSounds();
This way the sound will never play.
Finally, go to the first frame of your preloader layer and insert this code:
stop();
That's all you need to prepare to use the sound. Now we will make a button to actually hear it.
Create a keyframe in the second frame of the preloader layer. From the common libraries in Windows | Other panels | Common libraries, open the button library. Drag one of the buttons from the library and drag it under the preloader bar in the preloader layer in the 2 rd frame. Name the button instance ?playBtn?

For this button to play a sound you must create a sound object and attach the sound with its linkage id. To do this, insert this in the buttons actions:
on (release) {
var my_sound = new Sound();
my_sound.attachSound("music");
my_sound.start();
}
If you test your movie now, the button will never show up, right? That's because we have to tell the preloader to go to the second frame after the movie loads. For this, go to the loadBar actions and insert this code right before the ending braces:
if (this._xscale == 100) {
_root.nextFrame();
}
Now the movie will go to the second frame after the movie is completely loaded. Since the file is in your computer, this may not be evident, but you can simulate a download by pressing Ctrl+Enter while testing the movie. The preloader will fill up and then the button appears. Click it and voila! you will hear your sound.
Adding more sounds is very simple, just add one layer per sound in the folder where you'll keep them and make sure the sound only takes up only one frame and that they are all aligned.
There is only one consideration to this method, and that is that all sounds MUST appear before the frame in which they are actually used. Also, stopping the sounds is only necessary in one of the layers of the sounds folder and only if you're going to use that frame, because you could just as easily jump over that frame.
Well, that's all. Hope it is helpful.
Spread The Word
1 Response to "Sounds and preloaders" 
|
said this on 05 Jan 2011 1:18:08 PM CDT
Thank you so much! I look
|


Author/Admin)