EasyManua.ls Logo

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH - Page 637

MACROMEDIA FLASH 8-LEARNING ACTIONSCRIPT 2.0 IN FLASH
830 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
Sending and loading variables 637
} else {
trace("Unable to load XML");
}
};
my_xml.load("http://www.helpexamples.com/flash/xml/ds.xml");
pBar_mc.onEnterFrame = function() {
var pctLoaded:Number = Math.floor(my_xml.getBytesLoaded() /
my_xml.getBytesTotal() * 100);
if (!isNaN(pctLoaded)) {
pBar_mc.bar_mc._xscale = pctLoaded;
pBar_mc.label_txt.text = pctLoaded + "% loaded";
if (pctLoaded >= 100) {
pBar_mc.onEnterFrame = undefined;
}
}
};
The previous code is broken down into seven sections. The first section defines the width
and height of the progress bar when it is drawn on the Stage. The progress bar will be
centered on the Stage in an upcoming section. The next section of code creates two movie
clips,
pBar_mc and bar_mc. The bar_mc movie clip is nested inside pBar_mc, and draws a
red rectangle on the Stage. The
bar_mc instance modifies its _xscale property as the
external XML file loads from the remote website.
Next, a second movie clip is nested inside of the
pBar_mc movie clip, stroke_mc. The
stroke_mc movie clip draws an outline on the Stage that matches the dimensions
specified by the
barHeight and barWidth variables defined in the first section. The
fourth section of code creates within the
pBar_mc movie clip a text field that is used to
display what percentage of the XML file has already loaded, similar to the label on the
ProgressBar component. Next, the
pBar_mc movie clip (which includes the nested
bar_mc, stroke_mc, and label_txt instances) is centered on the Stage.
The sixth section of code defines a new XML object instance, which is used to load an
external XML file. An onLoad event handler is defined and traces a message to the Output
panel. The onLoad event handler also deletes the
onEnterFrame event handler (which is
defined in the next section) for the
pBar_mc movie clip. The final section of code defines
an
onEnterFrame event handler for the pBar_mc movie clip. This event handler monitors
how much of the external XML file has loaded and modifies the
_xscale property for the
bar_mc movie clip. First the onEnterFrame event handler calculates what percentage of
the file has finished downloading. As long as the percentage of the file loaded is a valid
number, the
_xscale property for bar_mc is set, and the text field within pBar_mc
displays what percentage of the file has loaded. If the file has completed loading (percent
loaded reaches 100%) the
onEnterFrame event handler is deleted so download progress is
no longer monitored.

Table of Contents

Related product manuals