View comments | RSS feed

Creating an application with the ProgressBar

The following procedure shows you how to add a ProgressBar component to an application while authoring. In this example, the ProgressBar uses the event mode. In event mode, the loading content emits progress and complete events that the ProgressBar dispatches to indicate progress. When the progress event occurs, the example updates a label to show the percent of content that has loaded. When the complete event occurs, the example displays "Loading complete" and the value of the bytesTotal property, which is the size of the file.

To create an application with the ProgressBar component in event mode:

  1. Create a new Flash file (ActionScript 3.0) document.
  2. Drag the ProgressBar component from the Components panel to the Stage.
  3. Drag the Button component from the Components panel to the Stage.
  4. Drag the Label component to Stage and give it an instance name of progLabel.
  5. Open the Actions panel, select Frame 1 in the main Timeline, and enter the following ActionScript code, which loads an mp3 audio file:
    import fl.controls.ProgressBar;
    import flash.events.ProgressEvent;
    import flash.events.IOErrorEvent;
    
    var aSound:Sound = new Sound();
    aPb.source = aSound;
    var url:String = "http://www.helpexamples.com/flash/sound/song1.mp3";
    var request:URLRequest = new URLRequest(url);
    
    aPb.addEventListener(ProgressEvent.PROGRESS, progressHandler);
    aPb.addEventListener(Event.COMPLETE, completeHandler);
    aSound.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
    loadButton.addEventListener(MouseEvent.CLICK, clickHandler);
    
    function progressHandler(event:ProgressEvent):void {
         progLabel.text = ("Sound loading ... " + aPb.percentComplete);
    }
    
    function completeHandler(event:Event):void {
        trace("Loading complete");
        trace("Size of file: " + aSound.bytesTotal);
        aSound.close();
        loadButton.enabled = false;
    }
    
    function clickHandler(event:MouseEvent) {        
        aSound.load(request);
    }
    
    function ioErrorHandler(event:IOErrorEvent):void {
        trace("Load failed due to: " + event.text);
    }
    
  6. Select Control > Test Movie.

The following example sets the ProgressBar to polled mode. In polled mode, progress is determined by listening for progress events on the content that is loading and using its bytesLoaded and bytesTotal properties to calculate progress. This example loads a Sound object, listens for its progress events, and calculates the percent loaded using its bytesLoaded and bytesTotal properties. It displays the percent loaded in both a label and in the Output panel.

To create an application with the ProgressBar component in polled mode:

  1. Create a new Flash file (ActionScript 3.0) document.
  2. Drag a ProgressBar component from the Components panel to the Stage and enter the following values in the Property inspector:
  3. Drag the Label component to the Stage and enter the following values in the Property inspector:
  4. Open the Actions panel, select Frame 1 in the main Timeline, and enter the following ActionScript code, which creates a Sound object (aSound) and calls loadSound() to load a sound into the Sound object:
    import fl.controls.ProgressBarMode;
    import flash.events.ProgressEvent;
    import flash.media.Sound;
    
    var aSound:Sound = new Sound();
    var url:String = "http://www.helpexamples.com/flash/sound/song1.mp3";
    var request:URLRequest = new URLRequest(url);
    
    aPb.mode = ProgressBarMode.POLLED;
    aPb.source = aSound;
    aSound.addEventListener(ProgressEvent.PROGRESS, loadListener);
    
    aSound.load(request);
    
    function loadListener(event:ProgressEvent) {
        var percentLoaded:int = event.target.bytesLoaded / event.target.bytesTotal * 100;
        progLabel.text = "Percent loaded: " + percentLoaded + "%";
        trace("Percent loaded: " + percentLoaded + "%");
    }
    
  5. Select Control > Test Movie to run the application.

The following example sets the ProgressBar to manual mode. In manual mode, you must set progress manually by calling the setProgress() method and provide it with the current and maximum values to determine the extent of progress. You do not set the source property in manual mode. The example uses a NumericStepper component, with a maximum value of 250, to increment the ProgressBar. When the value in the NumericStepper changes and triggers a CHANGE event, the event handler (nsChangeHander) calls setProgress() method to advance the ProgressBar. It also displays the percent of progress completed, based on the maximum value.

To create an application with the ProgressBar component in manual mode:

  1. Create a new Flash file (ActionScript 3.0) document.
  2. Drag the ProgressBar component from the Components panel to the Stage and give it the following values in the Property inspector:
  3. Drag a NumericStepper component to the Stage and enter the following values in the Property inspector:
  4. Drag a Label component to the Stage and enter the following values in the Property inspector:
  5. Open the Actions panel, select Frame 1 in the main Timeline, and enter the following code:
    import fl.controls.ProgressBarDirection;
    import fl.controls.ProgressBarMode;
    import flash.events.Event;
    
    aPb.direction = ProgressBarDirection.RIGHT;
    aPb.mode = ProgressBarMode.MANUAL;
    aPb.minimum = aNs.minimum;
    aPb.maximum = aNs.maximum;
    aPb.indeterminate = false;
    
    aNs.addEventListener(Event.CHANGE, nsChangeHandler);
    
    function nsChangeHandler(event:Event):void {
        aPb.value = aNs.value;
        aPb.setProgress(aPb.value, aPb.maximum);
        progLabel.text = "Percent of progress = " + int(aPb.percentComplete) + "%";
    }
    
  6. Select Control > Test Movie to run the application.
  7. Click the Up Arrow on the NumericStepper to advance the ProgressBar.

The following example creates a ProgressBar using ActionScript. Apart from that, it duplicates the functionality of the preceding example, which creates a ProgressBar in manual mode.

To create a ProgressBar using ActionScript:

  1. Create a new Flash file (ActionScript 3.0) document.
  2. Drag the ProgressBar component to the Library panel.
  3. Drag the NumericStepper component to the Library panel.
  4. Drag the Label component to the Library panel.
  5. Open the Actions panel, select Frame 1 in the main Timeline, and enter the following code:
    import fl.controls.ProgressBar;
    import fl.controls.NumericStepper;
    import fl.controls.Label;
    import fl.controls.ProgressBarDirection;
    import fl.controls.ProgressBarMode;
    import flash.events.Event;
    
    var aPb:ProgressBar = new ProgressBar();
    var aNs:NumericStepper = new NumericStepper();
    var progLabel:Label = new Label();
    
    addChild(aPb);
    addChild(aNs);
    addChild(progLabel);
    
    aPb.move(180,175);
    aPb.direction = ProgressBarDirection.RIGHT;
    aPb.mode = ProgressBarMode.MANUAL;
    
    progLabel.setSize(150, 22);
    progLabel.move(180, 150);
    progLabel.text = "";
    
    aNs.move(220, 215);
    aNs.maximum = 250;
    aNs.minimum = 0;
    aNs.stepSize = 1;
    aNs.value = 0;
    
    aNs.addEventListener(Event.CHANGE, nsChangeHandler);
    
    function nsChangeHandler(event:Event):void {
        aPb.setProgress(aNs.value, aNs.maximum);
        progLabel.text = "Percent of progress = " + int(aPb.percentComplete) + "%";
    }
    
  6. Select Control > Test Movie to run the application.
  7. Click the Up Arrow on the NumericStepper to advance the ProgressBar.

Flash CS3


Comments


Proteus1968 said on Aug 29, 2008 at 3:13 PM :
Are any of these example supposed to work? The first two do not! I'm sure I'm doing something wrong, but I cannot find one progressbar tutorial that works. All I want to do is create simple loading progress bar for a .swf file and in a week I haven't found one that works. I'm working on a template for a audio / image player that links to a MP3 that automatically loads when you start the .swf file. I don't need a start button or even a label (althought that would be OK). I just want something for the user to look at while a 20Mb+ MP3 loads.
lenno0ne said on Feb 23, 2009 at 3:38 PM :
I love this information very informative:

But for god sake Ive tried to find out information on this topic with no luck, your my only hope (OBI1) of trying to find out how to use this class and these methods to have multiple buttons that stream multiple sound files.

I thought that there at least must be somewhere you can advice me to start looking if you cant tell me how to do it?


I have managed to follow this tutorial sucessfully to achieve a single button that uses the component pre-loader and label.

I now want to use the preloader and label for all my 30 MP3's and 30 Buttons.

Its for an E-learning project for geography i want to pre-load the name of the country before it plays so there are no errors.

But I want to have as little code in as necessary.



thanks
e

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/9.0/main/00000455.html