...

View Full Version : Preloader for dynamically loaded images?



marilynn.fowler
06-15-2009, 11:53 PM
I have a an Actionscript 3 XML slideshow with a 1-second fade tween transition. My movie is one frame long. I would like the previous image to unload and the fade to occur AFTER the image is completely loaded. How do I accomplish this? It can either blink a circle have a simple download bar, but that's secondary.

Here's my code:



import fl.transitions.Tween;
import fl.transitions.TweenEvent;
import fl.transitions.easing.*;

stop();

//if it doesn't exist, make it, else unload it (coming from other frames)
if (!imgLoader) {
var imgLoader:Loader;
} else {
imgLoader.unload();
}

var fadeTween:Tween;

var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("photos.xml"));
var imgData:XML;

//keeps track of the image URL
var whichImg:String;

//keeps track of the image number
var imgNum:Number = 0;
var totalPics:Number;
var isFirst:Boolean = true;

var myTimer:Timer = new Timer(200);

//when xml is loaded, start timer, run the checker
function xmlLoaded(event:Event):void {
myTimer.start();
myTimer.addEventListener(TimerEvent.TIMER, checker);
imgData = new XML(event.target.data);
}

//loads image
function showPic():void{
myTimer.removeEventListener(TimerEvent.TIMER, checker);

whichImg = imgData.work.image[imgNum].imgURL;
totalPics = imgData.work.*.length();

imgLoader = new Loader;
imgLoader.load(new URLRequest(whichImg));
slideshow_mc.addChild(imgLoader);

fadeTween = new Tween(imgLoader, "alpha", None.easeNone, 0, 1, 2, true);
}

function checker(event:TimerEvent):void {
if (imgNum == 0) {
//if this is not the initial load, unload imgLoader
if (!isFirst) imgLoader.unload();
isFirst = false;
showPic();
} else if (imgNum < totalPics) {
imgLoader.unload();
showPic();
} else {
imgLoader.unload();
imgNum = 0;
showPic();
}
}

//when you click a button, go to the next/previous image
function clickHandler(event:MouseEvent):void {
switch(event.target) {
case next_btn:
myTimer.addEventListener(TimerEvent.TIMER, checker);
imgNum++;
break;
case prev_btn:
imgNum--;
myTimer.addEventListener(TimerEvent.TIMER, checker);
break;
}
if (imgNum == totalPics) {
imgNum = 0;
} else if (imgNum < 0) {
imgNum = totalPics - 1;
}
}

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);
next_btn.addEventListener(MouseEvent.CLICK, clickHandler);
prev_btn.addEventListener(MouseEvent.CLICK, clickHandler);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum