In this short tutorial i will show you how play music files in as2.
What do we need:
- 2 music files. One of them is a music and the other a sound effect.
- General knowlegde about as2 and flash.
At first import the 2 files in the library. Then select the music and right click > properties/attributes (ok, i have the german version, so i dont know what there is in the englisch version. Its the last item) > Check "Export for actionscript. Then give it the name "my_music". Do the same with the sound and give it the namy "my_sound".
Now you need some actionscript. We have to attach these 2 files to variables. We create a new sound:
Code:
_root.my_music = new Sound(_level0)
_root.my_sound = new Sound(_level0)
We will need following founction: attachSound( - name - )
Now we attach the files:
Code:
_root.my_music.attachSound("my_music")
_root.my_sound.attachSound("my_sound")
Now we have to start the music. We use the function: start( - secondOffset - , - loop)
Code:
_root.my_music.start(0,9999999) // this will play it in a endless loop
_root.my_sound.start(0,1) //this will play it one time
To stop use stop()
Code:
_root.my_music.stop()
_root.my_sound.stop()
Now we want to make a mute function. You can call this function with a button, key or context menu item.
We have a music and sound. We give the opinion to mute the music or sound. At first we need 2 controll variables.
Code:
_root.mute_music = false
_root.mute_sound= false
To mute we will set the volume to 0. But we have to make the function first.
Code:
function mute(mode){
if (mode == "music"){
if(_root.mute_music == false){
_root.mute_music = true
_root.my_music.setVolume(0)
}else{
_root.mute_music = false
_root.my_music.setVolume(100)
}
}else if (mode == "sound"){
if(_root.mute_sound == false){
_root.mute_sound = true
_root.my_sound.setVolume(0)
}else{
_root.mute_sound = false
_root.my_sound.setVolume(100)
}
}else{
trace("Parameter is not valid!")
}
}
This function has one parameter. It mutes and unmutes. If you input a wrong paramter, it traces a small text on the debug console.
Hope you learned something from this tutorial. Make sure you read my other tutorials.