Sound and loading progress bar

Source: Internet
Author: User

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 ();

    1. VaR button1: button1 = new button1 ();
    2. VaR button2: button2 = new button2 ();
    3. Button1.x = 50;
    4. Button2.x = 200;
    5. Button1.y = 100;
    6. Button2.y = 100;
    7. Addchild (button1 );
    8. Addchild (button2 );
    9. Button1.addeventlistener (mouseevent. Click, playinternal );
    10. Button2.addeventlistener (mouseevent. Click, playexternal );
    11. Function playinternal (Event: mouseevent)
    12. {
    13. VaR sound1: sound1 = new sound1 ();
    14. VaR channel: soundchannel = sound1.play ();}
    15. Function playexternal (Event: mouseevent)
    16. {
    17. VaR sound2: Sound = new sound ();
    18. VaR Req: URLRequest = new URLRequest ("externalfailed ");
    19. Sound2.load (req );
    20. Sound2.play ();
    21. }

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 >}

    1. stop ();
    2. addeventlistener (event. enter_frame, loadprogress);
    3. function loadprogress (Event: Event) {
    4. // get bytes loaded and bytes total
    5. var moviebytesloaded: Int = This. Root. loaderinfo. bytesloaded;
    6. var moviebytestotal: Int = This. Root. loaderinfo. bytestotal;
    7. // convert to kilobytes
    8. var moviekloaded: Int = moviebytesloaded/1024;
    9. var moviektotal: Int = moviebytestotal/1024;
    10. // show progress
    11. progresstext. Text = "loading:" + moviekloaded + "K/" + moviektotal + "K";
    12. // move on if done
    13. If (moviebytesloaded> = moviebytestotal) {
    14. removeeventlistener (event. enter_frame, loadprogress);
    15. gotoandstop (2);
    16. }
    17. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.