Adding Sounds
Now that the grid is generated, you can attach the sounds (all 20 of them) from the Library to each box. Before you start writing the code, you need to reference each sound by assigning a Linkage ID to it.
Open the sounds folder in the Library.
Select the first one, ARIA_S1_MP3, as shown in Figure 6.
Right-click/Ctrl-click and select the Linkage option.
In the Linkage Properties panel, select Export for ActionScript.
Type in the identifier for the Linkage ID: sound0.
Repeat Steps 35 for the remaining 19 sounds in the Library, where ARIA_S2.MP3 has linkage ID sound1, ARIA_S3.MP3 has sound2, and so on.
Select the script layer (main movie). Click on Frame 1.
Enter this code right after your movieGrid development code and before the last }:
Save your work.
Listing 03
//Attaching sounds movieGrid[boxName].sound = new Sound(); movieGrid[boxName].soundNum = soundNum; movieGrid[boxName].sound.attachSound("sound"+soundNum); soundNum++;
Your actions for this frame should now look like this:
onLoad = function(){ //constants _global.ROWS = 4; _global.COLUMNS = 5; _global.BOXWIDTH = 100; _global.BOXHEIGHT = 40; _global.DEFAULTBOXALPHA = 30; _global.SPEED = 5; //variables var x,y :Number = 0; var soundNum :Number = 0; //Grid development createEmptyMovieClip("movieGrid", 1); //Set initial volume movieGrid.soundVolume = 100; movieGrid.gridColumnCount = 0; //Attaching the individual cells of the grid. //Setting height and width. for(x=0;x<ROWS;x++) { for(y=0;y<COLUMNS;y++) { var boxName:String = "boxMC"+x+"_"+y; movieGrid.attachMovie("boxMC",boxName,10*x+y); movieGrid[boxName]._width = BOXWIDTH; movieGrid[boxName]._height = BOXHEIGHT; movieGrid[boxName]._x = movieGrid[boxName]._width * y; movieGrid[boxName]._y = movieGrid[boxName]._height * x; movieGrid[boxName].boxLight._alpha = DEFAULTBOXALPHA; //Attaching sounds movieGrid[boxName].sound = new Sound(); movieGrid[boxName].soundNum = soundNum; movieGrid[boxName].sound.attachSound("sound"+soundNum); soundNum++; } } }
Each time through the loop, an instance of the boxMC movie clip (located in the movie's Library) will be placed on the Stage and given an instance name and level based on its row and column. After this instance is on the Stage, you can create a new sound object, and by using the attachSound() method, you can attach one of the Library's sounds to it. (These will be named sound0sound19 in this movie.)
By using sound objects, you don't have to drag the sound from the Library onto the Stage, and you can use the methods and properties of the sound object to manipulate your sounds. See Figure 8.
Finally, you add one to the soundNum variable, so the next sound attached is the next in the sequence. The sounds, when attached, will follow this pattern:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19