Recipe 16.5. Hiding a Progress Bar When Content Has Loaded


Problem

You want to determine when content has completed loading so that you can tell Flash to hide the progress bar.

Solution

If you are using the Flash 8 Cookbook ProgressBar, set the autoHide parameter to TRue.

Otherwise, if you are using the Macromedia v2 ProgressBar, add a listener object, and when the complete( ) method is called, have Flash set the ProgressBar instance's visible property to false.

Alternatively, if you are using a custom load indicator as discussed at the end of Recipe 16.4, add a listener object to the MovieClipLoader object, and when the onLoadInit( ) method is called, have Flash set the movie clip and/or text field's _visible property to false.

Discussion

Typically, when content has completed loading, you will want to hide the ProgressBar or other graphical indicator that you have used to display the load progress to the user. If you are using the Flash 8 Cookbook ProgressBar, all you need to do is set the autoHide parameter to TRue. Then, when the percent loaded has reached 100%, the instance will automatically hide.

If you are using the Macromedia v2 ProgressBar, your job is a little more complex. In order to accomplish this task, you'll need to use a little bit of ActionScript. But you needn't worry: the ActionScript is fairly short and simple.

If you want to hide a ProgressBar component instance after the content has loaded, complete the following steps:

  1. Create an event listener object. As with the other types of listener object discussed in this book, there are many different types of objects that could be used, but for the purposes of keeping the example simple and functional, I will use an instance of the Object class. Therefore, to create the object, use the following code as an example:

     var oLoadedListener:Object = new Object(); 

  2. When the content has loaded, the ProgressBar will dispatch a complete event and the listener object's complete( ) method is automatically called. Therefore, you'll want to add a complete( ) method to the listener. The following code shows how to add the method to the listener object (we'll look at the code that goes within the method in the next step):

     oLoadedListener.complete = function(oEvent:Object):Void { }; 

  3. Within the complete( ) method, set the ProgressBar instance's visible property to false. You can reference the ProgressBar instance in one of several ways. The complete( ) method is passed a parameter that has a reference to the ProgressBar. You can retrieve that reference by way of the target property of that parameter. Therefore, you should define the code within the complete( ) method as follows. (The code shown includes the code from the previous step, with the new code shown in boldface.)

     oLoadedListener.complete = function(oEvent:Object):Void {    oEvent.target.visible = false; }; 

  4. The remaining step is to add the listener object as a listener for the ProgressBar instance. You should call the addEventListener( ) method from the ProgressBar instance in order to do that. Assuming that you have named the ProgressBar instance pbIndicator, then you can use the following code to register a listener object named oLoadedListener to listen for the complete event.

     pbIndicator.addEventListener("complete", oLoadedListener);  

If you are using a custom (non-ProgressBar) load indicator in conjunction with MovieClipLoader, you can hide the indicator with the following steps:

  1. If you haven't already, complete steps 1 through 5 from the discussion of using MovieClipLoader to monitor load progress in Recipe 16.4.

  2. Add an onLoadComplete( ) method to the listener object. The onLoadComplete( ) method is automatically called when the content has loaded completely. The following code shows the basic method declaration (I'll discuss the content of the method in the next step):

     oMlListener.onLoadComplete = function(mContent:MovieClip):Void { }; 

  3. Within the onLoadComplete( ) method definition, set the _visible property to false for any and all load indicator movie clips and/or text fields that you have used. For example, if you have a load indicator bar movie clip named mcLoadIndicator and a text field named tLoadIndicatorLabel, than you can use the following onLoadComplete( ) declaration (new code shown in boldface):

     oMlListener.onLoadComplete = function(mContent:MovieClip):Void {    mLoadIndicator._visible = false   tLoadIndicatorLabel._visible = false; }; 

See Also

Recipe 16.4




Flash 8 Cookbook
Flash 8 Cookbook (Cookbooks (OReilly))
ISBN: 0596102402
EAN: 2147483647
Year: 2007
Pages: 336
Authors: Joey Lott

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net