| Maintain Multiple, Independent Sound Object Properties Across Multiple Scenes | Return to Index | Do you want all FLA's used for this tutorial? | Do you have a question? |
Have you ever needed to have one sound object loop at a particular volume level across multiple scenes while starting and stopping other sounds that may have different volume levels? Frankly, I used to think it was impossible over multiple scenes with sound objects.
Frankly, I rarely use scenes in my Flash programming, but I know that many like to organize movies that way, and I get asked this question from time to time.
In a nut shell, this approach involves creating your sound object in its own FLA and publishing the SWF for that sound object. You will then import that SWF into your main movie (using loadMovie or loadClip) during your normal preloading events. It requires a separate SWF for each sound, so if you have 20 sounds you wish to control in this manner, you will have 20 SWF's to load. Think it through, as you may have 20 sounds but only need independent control of 5 of them.
This will allow you to maintain control of all the methods and properties that sound objects provide such as position, duration, onSoundComplete, etc.
Below is a sample of what this tutorial will produce. The following is a 4 scene movie that can play 4 audio files that were created as outlined below. By pressing the green button in any scene, you can start the sound object looping. The red button will stop it. The buttons at the bottom allow you to change the volume levels. You will see that you can have unique volume levels for all your sounds across all 4 scenes. You can start and stop the sound objects and still maintain the levels. You can track the levels across scene changes as well.
How It Works
This solution allows you to associate each sound object with its own movie in your main FLA (which is what provides independent control of each sound). These will not be movies you have on stage on the timeline. These will be movies you create using the createEmptyMovieClip method. If you use actual movies, like an empty movie you drag out from the library, you will lose independent control of the sound when you change scenes. The reason this happens is because the movie you so lovingly placed on the timeline of scene 1 does not exist in scene 2, etc. Even if you place the same movie in scene 2, it will not cooperate like you want. Follow the steps below for a working solution.Creating the Sound Object SWF in Its Own FLA
This process is similar to the steps you would take if you want to
preload sound objects that use the
attachSound method. Do the following:
Create a new FLA blank document.
Make the document a small size. I often make these FLA's 5-pixels square. However, you may decide to make the document large enough to add a small button for testing the sound. After you loadMovie the SWF, you can move its X,Y axis off stage so it will not be seen. I'll show code for that in this tutorial.
| _root.sound1 = new Sound(this); _root.sound1.attachSound("sound1a"); |
Prepare the Master FLA to Use the Sounds
In the following steps, we will create empty movies clips ("containers") dynamically and then load the sound SWF's into these movies.
Decide on a Preload Method
I recommend the MovieClipLoader class as a convenient way to handle pre-loading all of these files. The traditional preloader would require the use of loadMovie or loadMovieNum to load the sound SWF's. The MovieClipLoader class has its own method called loadClip. The MovieClipLoader method is shown below.
The follow assumes that nothing is on frame 1 of scene 1 except the preloader and anything else needed to support the preloader graphically. In this example, we are loading 4 sound SWF's. However, the pattern should be obvious enough so that you can reproduce this example for more or fewer sounds.
if (_root.soundLoaded != 1) { // This (the above) will keep it from loading again if you return to stop(); // Make MovieClipLoader --------------------------
};// Close onLoadStart _root.preload.onLoadComplete = function(targetMC) {
containersLoaded += targetMC.getBytesTotal(); // Move the loaded SWF's off stage };// Close onLoadComplete //------------------------------------------------- |
Notice that in the above code, the X axis of the sound SWF is moved off stage after it loads. This is so it will not be seen during playback.
Make the containers for the sounds
In this example, I show how to load 4 sound SWF's. Follow the pattern to create the number of containers needed for your sounds. The following code should be placed on frame 1 of the _root timeline, just below the previous code.
// Make sound containers _root.createEmptyMovieClip("container1", getNextHighestDepth()); |
Update the Preloader
this.onEnterFrame = function() {
}
} } // Close onEnterFrame }// Close if _root.soundLoaded != 1 |
The above concludes the tutorial.
|
Return to Index | Do you want all FLA's used for this tutorial? | Do you have a question? |
This document copyright © 2006 by Kenny Bellew of Cowfly.Com Design, kennybellew@hotmail.com