Playing sounds
There are two main methods to play the sound, calling the library or calling external files
For most games, the best way is to call the sound in the library. You can pour the sound into the flash library.
To use this sound, you need to set the linkage, fill in the class name, and then we canCodeUse this name to control the sound
We name it sound1
Then play the sound. Only two lines of code are required.
VaR sound1: sound1 = new sound1 ();
VaR channel: soundchannel = sound1.play ();
We can also use a line to represent
VaR channel: soundchannel = (New sound1 (). Play ();
Playing an external sound is a little complicated. First, you need to load the sound into an object.
VaR sound2: Sound = new sound ();
VaR Req: URLRequest = new urlrequest(“playingsounds.mp3 ");
Sound2.load (req );
Then, use the Playback command to play the sound.
Sound2.play ();
- VaR button1: button1 = new button1 ();
- VaR button2: button2 = new button2 ();
- Button1.x = 50;
- Button2.x = 200;
- Button1.y = 100;
- Button2.y = 100;
- Addchild (button1 );
- Addchild (button2 );
- Button1.addeventlistener (mouseevent. Click, playinternal );
- Button2.addeventlistener (mouseevent. Click, playexternal );
- Function playinternal (Event: mouseevent)
- {
- VaR sound1: sound1 = new sound1 ();
- VaR channel: soundchannel = sound1.play ();}
- Function playexternal (Event: mouseevent)
- {
- VaR sound2: Sound = new sound ();
- VaR Req: URLRequest = new URLRequest ("externalfailed ");
- Sound2.load (req );
- Sound2.play ();
- }
Copy code
Loading screen
Flash is streaming media, even though flash is not completely downloaded! You only need to download a little bit of animation to start playing.
This function is good! Users do not have to wait, but the game is different, because the game requires all the game elements to be downloaded.
So that you can use a loading screen to force all the video elements to be downloaded before playing.
The simple method is to add a stop () in the first frame ();
Then we set an Enter frame listener to call the loadprogress function.
Addeventlistener (event. enter_frame, loadprogress );
This function can get the video status by setting this. Root. loaderinfo, through bytesloaded and bytestotal
Get the number of bytes downloaded from the video and the total number of bytes of the video.
Function loadprogress (Event: Event ){
// Get bytes loaded and bytes total
VaR moviebytesloaded: Int = This. Root. loaderinfo. bytesloaded;
VaR moviebytestotal: Int = This. Root. loaderinfo. bytestotal;
// Convert to kilobytes
VaR moviekloaded: Int = moviebytesloaded/1024;
VaR moviektotal: Int = moviebytestotal/1024;
Demonstrate the process of downloading to users
Progresstext. Text = "loading:" + moviekloaded + "K/" + moviektotal + "K ";
When the number of downloaded bytes is equal to the total number of bytes of the video, we delete the listener and start playing the video.
// move on if done
If (moviebytesloaded> = moviebytestotal) {
removeeventlistener (event. enter_frame, loadprogress);
gotoandstop (2);
}< BR >}
- stop ();
-
- addeventlistener (event. enter_frame, loadprogress);
-
- function loadprogress (Event: Event) {
- // get bytes loaded and bytes total
- var moviebytesloaded: Int = This. Root. loaderinfo. bytesloaded;
- var moviebytestotal: Int = This. Root. loaderinfo. bytestotal;
-
- // convert to kilobytes
- var moviekloaded: Int = moviebytesloaded/1024;
- var moviektotal: Int = moviebytestotal/1024;
-
- // show progress
- progresstext. Text = "loading:" + moviekloaded + "K/" + moviektotal + "K";
-
- // move on if done
- If (moviebytesloaded> = moviebytestotal) {
- removeeventlistener (event. enter_frame, loadprogress);
- gotoandstop (2);
- }
- }