First, sorry. Awhile back I posted my code to preload multiple images that can be found here:
http://manewc.com/2008/05/16/preloading-multiple-images-with-actionscript/
I had a request to demo how the code works, so as I looked through the code I realized that one of the arrays should have been displayed as public instead of private to make the code more reusable. Anyways, I appended some code here to hopefully clarify some issues anyone may have encountered.
Here is the updated movie:
Here are the files that I used for this demo:
Here is the updated code:
package
{
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.TimerEvent;
import flash.utils.setInterval;
import flash.utils.clearInterval;
public class MultipleImagePreload extends Sprite
{
public var images:Array;
public var chkLoaded:Boolean = false;
private var imgArray:Array;
// array of images.. onload event
private var imgLoadArray:Array;
// array of LoadImage class
public var loadArray:Array;
// variables for our timer events
private var initLoadTimer:uint;
private var preLoadTimer:uint;
// variables for the preload process
private var getTotalBits:Number; // the tally for the total bits
private var imageTotalSize:Number;
private var imagePreLoadSize:Number; // the tally for preloading
public function MultipleImagePreload(images:Array)
{
imgArray = images;
// Load in the images.
loadArray = new Array();
for ( var i:uint; i < images.length; i++ )
{
// load the image
loadArray[i] = new LoadImage( imgArray[i] );
// display the image to stage
addChild ( loadArray[i] );
// send them off the stage
loadArray[i].x = -9999;
}
initLoadTimer = setInterval(onLoadImage, 500);
}
private function onLoadImage():void
{
var imgLoadArray = new Array();
for ( var i:uint=0; i < imgArray.length; i++ )
{
getTotalBits = loadArray[i].getBits();
if ( getTotalBits > 0 )
{
// check again.
imgLoadArray[i] = loadArray[i];
}
}
if ( imgLoadArray.length == imgArray.length )
{
clearInterval ( initLoadTimer );
imageTotalSize = 0;
// add up the size of all images
for ( var u:uint=0; u < imgArray.length; u++ )
{
if ( loadArray[u].getBits() ) /* Safari is requiring this */
{
imageTotalSize += loadArray[u].getBits();
}
}
Main(root).Message( "Total Size (bits): " + imageTotalSize + "\n" );
// we now have recorded all file sizes.. now start the preloading
preLoadTimer = setInterval(preLoadImage, 50);
}
}
private var loadStat:Number;
private function preLoadImage():void
{
imagePreLoadSize = 0;
// add up the size of all images
for ( var p:uint=0; p < imgArray.length; p++ )
{
imagePreLoadSize += loadArray[p].getBitsLoaded();
}
if ( ( imagePreLoadSize / imageTotalSize ) )
{
loadStat = Math.floor(100 * (imagePreLoadSize / imageTotalSize));
if ( loadStat >= 100 )
{
// clear the timer
clearInterval ( preLoadTimer );
// set the boolean variable to true to notify all images are loaded
chkLoaded = true;
Main(root).Message ("100% Loaded\n");
}
else
{
Main(root).Message (Math.floor(100 * (imagePreLoadSize / imageTotalSize)) + "% Loaded\n");
}
}
}
}
}
LoadImage.as:
package {
import flash.display.Loader;
import flash.display.Sprite;
import flash.events.*;
import flash.net.URLRequest;
public class LoadImage extends Sprite {
private var url:String;
private var imageHolder:Sprite;
public var bits:Number;
public var bitsLoaded:Number;
public var totalBits:Number;
public function LoadImage(url:String) {
var loader:Loader = new Loader();
configureListeners(loader.contentLoaderInfo);
var request:URLRequest = new URLRequest( url );
loader.load(request);
imageHolder = new Sprite();
addChild ( imageHolder );
imageHolder.addChild(loader);
}
private function configureListeners(dispatcher:IEventDispatcher):void {
dispatcher.addEventListener(ProgressEvent.PROGRESS, progressHandler);
dispatcher.addEventListener(Event.COMPLETE, completeHandler);
}
private function completeHandler(event:Event):void {
Main(root).Message( "Image Load Event Complete\n" )
}
public function getBits()
{
return ( bits );
}
public function getBitsLoaded()
{
return ( bitsLoaded );
}
private function progressHandler(event:ProgressEvent):void {
bitsLoaded = event.bytesLoaded;
bits = event.bytesTotal;
}
}
}
.. and the main class, Main.as
package {
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.text.TextFormat;
import flash.text.TextField;
import fl.controls.TextArea;
import fl.controls.ScrollPolicy;
public class Main extends Sprite
{
// array of all images to be prelaoded before the movie begins:
private var imgArray:Array;
// the class to handle all the images
private var imgPreload:MultipleImagePreload;
//text area component
private var output:TextArea;
public function Main()
{
stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;
showOutput();
init();
}
private function init():void
{
imgArray = new Array();
imgArray[0] = "http://manewc.com/projects/flash/i/ski-ryan.jpg";
imgArray[1] = "http://manewc.com/projects/flash/i/ski-ryan.jpg";
imgPreload = new MultipleImagePreload(imgArray);
addChild ( imgPreload );
stage.addEventListener ( Event.ENTER_FRAME, checkPreload );
}
private function checkPreload(e:Event):void
{
if ( imgPreload.chkLoaded )
{
Message ( "\n====================\nALL IMAGES LOADED\n====================\n" );
stage.removeEventListener ( Event.ENTER_FRAME, checkPreload );
Message ( "\n\n" );
Message ( "The way that this code works is that the images actually are
positioned off the screen to allow you to position them in the viewable
stage when you need them" );
Message ( "\n" );
Message ( "You target each movie via the array:\n" );
Message ( "\n" );
Message ( "For Example:" );
Message ( "\n" );
Message ( "imgPreload.loadArray[0].x = 200;" );
Message ( "imgPreload.loadArray[0].y = 0;" );
Message ( "\n" );
imgPreload.loadArray[0].x = 200;
imgPreload.loadArray[0].y = 0;
Message ( "Dimensions:" );
Message ( "\n" );
Message ( "------------------" );
Message ( "\n" );
Message ( "Width: " + imgPreload.loadArray[0].width );
Message ( "\n" );
Message ( "Height: " + imgPreload.loadArray[0].height );
}
}
private function showOutput():void
{
output = new TextArea();
output.verticalScrollPolicy = ScrollPolicy.ON;
output.condenseWhite = true;
output.setSize(200, stage.stageHeight);
output.move(0, 0);
addChild(output);
}
public function Message(msg:String):void
{
output.appendText ( msg );
}
}
}
Hello
How to jump on next image
Amazing, i have been struggeling for over weeks on how to preload al mij images and then continue. I really wanted to try myself, but thanks for the help!!
Excellent Code. Thanks for the effort you’ve put into this.
Compillation error occurs with TextArea and scrollpolicy. I have just copy pasted and tried out the example. what might be the reason?
Sounds like you will need to add the TextArea and ScrollBar components to your library inside your .fla file. If you download the files here and open up MultipleImagePreload.fla and look in it’s library you will see what I mean.
Thanks!
Cool.. Really thanks for pointing it out. It works now.
Thanks,
Balashankar.S