For example, we have a main flash called main.swf, and then there is a load.swf, as long as you write the following code in load.swf to download main.swf playback:
The code is as follows |
Copy Code |
var request : URLRequest = new URLRequest ("main.swf"); var loader:loader = new Loader (); Loader.contentLoaderInfo.addEventListener (progressevent.progress, loadprogress);//This is a loading process event Loader.contentLoaderInfo.addEventListener (Event.complete, loadcomplete);//This is the loaded event Function loadprogress ( event:progressevent): void { var percentloaded:number = event.bytesloaded/event.bytestotal ;//Calculate load percent, current load byte divided by total byte percentloaded = Math.Round (percentloaded * 100);//Convert to percent Trace ("Loading:" +percentloaded+ "%"); } Function LoadComplete (event:event): void { trace ("Complete"); & nbsp; loader.load (Request); addChild (loader);//load complete display to stage |
The above code is a good solution to the problem of loading main.swf display progress.
Of course, this is not our focus today, and we're going to focus today on a single SWF file that shows progress.
For example, there is a 1.5MB SWF, how to show the download progress? Here we have to make up a common sense, perhaps a lot of people know, that is, flash must download a frame before it will show the content of this frame, but in practice often many people do not really understand, the following we will specifically say.
Now that you've downloaded a frame to display, the production of loading must have at least 2 frames, the first frame after downloading, began to show progress, but many times, we found that the actual did not show, and we are really practical 2 frames, this time, we will check the size of the first frame.
There are times when producers don't pay attention, the first frame is very large, such as 1.2MB, the second needle is very small, 0.3MB, so, because it is downloaded after the first frame to show progress, and so download this 1.3MB, what is late, so we have to make the first frame as small as possible.
We need the debug version of Flash Player as our accessibility tool, as shown in the following illustration, to select the menu view-> Data flow icon:
Here, we can see that the red box shows the size of the first frame, 1248K, the red circle section represents the frame selection, where the first frame is selected. The first frame so big, no wonder will not show loading, such as display loading, the film is also basic download finished, so that loading is to the meaning.
Let's look at the same second frame:
Figure 2
The second frame is only 9 B, and is almost instantly downloaded.
So, all we have to do is make the first frame as small as possible and move all the data to the second frame, but how do we do that?
In the Flash IDE, select the file-> publish settings->flash and select script settings:
Figure 3
Set the "class in the export frame" to the second frame, and then save OK.
Figure 4
So, we put all the data in the second frame, and the first frame is much less:
Figure 5
As shown above, the first frame has been changed from 1248K to 119K, why 119K is because my first frame loaded a background loading picture, so big a bit, if there is no component of the words will be a lot smaller, only more than 10 K.
This way, when the first frame is downloaded, we can show the download progress, we write the code directly on the first frame:
The code is as follows |
Copy Code |
Stop (); Don't forget this, or you'll just skip to the second frame when you've finished downloading Root.loaderInfo.addEventListener (progressevent.progress,loadprogress,false,0,true) Root.loaderInfo.addEventListener (event.complete,init,false,0,true) Function loadprogress ( e:progressevent):void{ //loading process calls this function //e.bytesloaded/e.bytestotal are already loaded bytes with total bytes //need to show progress can be done here lotxt.text=string (int (e.bytesloaded/e.bytestotal*100)) + "%"; Lotxt is a dynamic text on the stage to display the download percentage } function init (e:event):void{ //loading complete, we skip to the second frame for playback gotoandstop (2); } |