With the Actions panel open, select Frame 1 of the Actions layer and add the following script:
stop(); onEnterFrame = function() { var framesLoaded = (Math.ceil (( _parent._framesloaded / _parent._totalframes) * 100)); gotoAndStop (framesLoaded); info_txt.text = framesLoaded + "% completed"; if (framesLoaded >= 90) { _root.gotoAndPlay ("Start"); } }
The first action prevents the timeline from moving forward until we tell it to do so.
The second line attaches an onEnterFrame event handler to the preloader clip. As a result, the script within the event handler is executed 24 times a second, which means that it instantly reacts to the current downloading conditions to display the most accurate representation of the process.
This script determines a percentage value for the number of frames that have downloaded. It then rounds up that number and assigns the resulting value to the framesLoaded variable. Using precedence, the expression employed to do this work is evaluated in the following manner:
_parent._framesloaded /_parent._totalframes
With this part of the expression, the number of frames on the main timeline that have loaded is divided by the total number of frames on the main timeline.
NOTE
Because this script is within the preloader movie clip instance and the instance resides on the main timeline, the use of _parent as the target path is a reference to the main timeline. This setup allows the preloader clip to be used (and function properly) without modification in any project.
For demonstration purposes, let's assume that the movie has 735 frames, 259 of which have loaded. The expression would look like this:
259 / 735
This would result in a value of .3523. The next part of the expression multiplies that result by 100:
* 100
This would result in a value of 35.23. Finally, using the Math.ceil() method, this value is rounded up to the next whole number, 36 and thus framesLoaded is assigned a value of 36. Remember that because this script is executed 24 times per second, the value of framesLoaded increases as the movie is downloaded.
TIP
The _framesloaded and _totalframes properties used in this script can be replaced with the getBytesLoaded() and getByteTotals() methods of the MovieClip class if you want to make this preloader react to bytes loaded (rather than just frames). This is sometimes the preferred method of scripting a preloader because a frame is not considered loaded (and thus the preloader doesn't advance) until all the data that it contains is loaded. The preloader may appear stalled if the frames loaded contain numerous bytes of data. In contrast, looking at the bytes loaded causes the preloader to move forward more smoothly because changes in byte data happen more frequently (as each byte of data is downloaded). See the bonus file on the CD that demonstrates this principle.
The next action in the script sends the preloader instance to the appropriate frame, based on the value of framesLoaded. Because the current movie contains the tweened animation of the progress bar moving from left to right, this action controls the movement of that bar. As the value of framesLoaded increases, so does the appearance of progress on the progress bar.
The next action sets what's displayed in the info_txt text field. Here, the value of framesLoaded is concatenated with the string "% completed". If framesLoaded has a value of 36, for example, the result is "36% completed".
The last part of the script contains a conditional statement used to determine when the preloader's work is complete. It says that when the value of framesLoaded is equal to or more than 90, the main timeline should be moved to the frame labeled Start and played from there.
NOTE
The value of 90 in this conditional statement could easily be changed to any value to specify when the preloader's work is finished and the movie can begin to play.