Recipe 16.4. Determining Load ProgressProblemYou want to monitor the load progress for content you are loading with MovieClipLoader, Loader, Window, or ScrollPane. SolutionUse the Macromedia v2 ProgressBar component. Alternatively, for a smaller file size, use the Flash 8 Cookbook ProgressBar component. If you want a completely programmatic solution, and you have used MovieClipLoader to load the content in the first place, you can use a listener object with MovieClipLoader to monitor the load progress. DiscussionWhen you are loading SWF and/or JPEG content into your Flash application, one important consideration is the time it will take to load the content. Most often, you want to make sure the user is notified as to what is going on as content loads. For example, if you are loading a 50KB .swf file and the user is connecting to your application over a dial-up connection, the content will not appear right away. In the interim, you want to make sure that the user at least knows that the content is loading. Otherwise he or she may be led to think that an error has occurred. Macromedia provides a ProgressBar component that you can use to monitor the load progress and display that progress to the user. To use the Macromedia ProgressBar with a Loader, ScrollPane, or Window component instance, complete the following steps:
If you are using the ProgressBar to monitor content loaded with MovieClipLoader, you can still use the ProgressBar component, but you need to make a few slight adjustments. The components, such as Loader, ScrollPane, and Window, dispatch events notifying any listening objects (such as a ProgressBar) as the content loads. However, content loaded with MovieClipLoader does not work in that way. Instead, the ProgressBar has to continually request the load progress from the movie clip into which the content is loading. In order to use a ProgressBar component in this manner, first complete the first two steps from the previous list, and then complete the following two steps in addition:
One thing that you may notice about the Macromedia v2 ProgressBar is that it alone adds 27KB to the file size of an .swf file. A ProgressBar component that is 27KB is, for many web-based projects, rendered useless. It is likely useful for web-based projects only if the audience has broadband connections and the content being loaded is quite large. Otherwise, the ProgressBar component needs to be smaller in file size to be effective. The Flash 8 Cookbook ProgressBar functions much like the Macromedia v2 ProgressBar, but it is significantly smaller in file size. If that is an issue for your project, you can consider downloading and using the Flash 8 Cookbook ProgressBar instead of the Macromedia version. The Flash 8 Cookbook ProgressBar works in the same way as that outlined in the preceding instructions.
If you want a completely programmatic solution for monitoring load progress, and if you are using MovieClipLoader to load the content, you can use some ActionScript code instead of a ProgressBar component. It is important to understand that with a completely programmatic solution, you will be responsible for providing any graphical content that you want to display to the user. The ProgressBar displays both a graphical and textual load indicator by default. With a completely programmatic approach, you will need to supply either of these elements. Before looking at how to coordinate any graphical elements with the programmatic solution, let's first look at the basic way in which you can use ActionScript to monitor the progress of loading content. The code-based solution works by way of a listener object. A listener object is an object that you register with, in this case, the MovieClipLoader instance that is managing the loading. The MovieClipLoader instance then dispatches events to the listener object that tell the listener when certain types of things have occurred. The event we'll look at in this recipe is the progress event. Each time more bytes of data are loaded, the MovieClipLoader dispatches the event and calls the onLoadProgress( ) method on the listener object. You can define the onLoadProgress( ) method such that you can tell Flash to perform certain tasks each time more bytes are loaded. For example, you could tell a graphical load indicator to update to reflect the amount of data that has loaded. If that sounds somewhat complicated, don't worry: it's actually much simpler than it sounds. Let's look at the steps, and examine each of them a little more carefully:
When you understand the basic way in which programmatic load progress monitoring works, the only other step is to coordinate that with graphical and/or textual elements that update to display the load progress to the user. One of the most common graphical elements is a load indicator bar that fills from left to right, just as with the ProgressBar component. You can achieve that same result by updating the _xscale property of a movie clip with horizontal bar artwork placed within it from within the onLoadProgress( ) method of the listener object. For example, assuming that you have created a movie clip instance named mLoadIndicator on the same timeline from which you have defined oMlListener, the following onLoadProgress( ) method definition will update the scaling of mLoadIndicator so that it coordinates with the load progress: oMlListener.onLoadProgress = function(mContent:MovieClip, nBytesLoaded:Number, nBytesTotal:Number):Void { mLoadIndicator._xscale = nBytesLoaded/nBytesTotal * 100; }; See AlsoRecipe 16.1 |
Tripod >> 3pod Tips & Learning and manuals for educations