Recreate a Google Swiffy animation once destroy() is called - google-swiffy

I'm trying to remove a Google swiffy (v 5.2.0) animation and then readd it at a later date.
In terms of the running animation there doesn't seem to be any problem, but the code triggers an error: TypeError: Cannot redefine property: Animation_fla.MainTimeline at which point all AS3 in the movie stops working. This seems to be because the destroy method is not removing references to the AS3 code within the swiffy runtime. I've spent some time trying to step through the code but it's pretty incomprehensible.
Below is a stripped out version of all I'm doing with swiffy - calling init again after calling destroy will trigger this TypeError. I've tried to reinitialise the swiffy runtime itself but this causes a similar error.
var stage;
function init() {
stage = new swiffy.Stage(domElement, swiffyJson);
stage.start();
}
function destroy() {
stage.destroy();
stage = null;
}

The only solution I've come up with is a pretty horrible hack. It seem Swiffy really doesn't like to recreate animations after they've been destroyed. I've had some success with detaching the Swiffy from the DOM but retaining it's reference in memory. I then had to hack the runtime to enable pause and restart. I could then reattach the original Swiffy without having to destroy it. This is in v5.2.0 downloaded from https://www.gstatic.com/swiffy/v5.2/runtime.js, after putting the runtime through jsbeautifier.org
Around line 5640 after the M.releaseCapture function I added the following function:
M.hackPause = function (bool) {
if(this.hackPaused === bool) return;
if(this.hackPaused) {
bi(ef(this.qh.yl, this.qh));
}
this.hackPaused = bool;
};
The around line 7137, replace the AK[I].yl function with the following:
Ak[I].yl = function () {
if (this.bh) {
var a = Date.now();
a >= this.Mf && (this.tl.ei(), this.Mf += (s[Xb]((a - this.Mf) / this.sl) + 1) * this.sl);
this.tl.lc();
if(!this.tl.hackPaused) {
bi(ef(this.yl, this))
}
}
};
What this is doing is preventing the requestAnimationFrame or setTimeout from firing and therefore effectively pausing the animation.
I've tried to expose a gotoAndStop function within the runtime also, but I couldn't manage to find the scope amongst the code. In the meantime, using a hack from this post - Is it possible to pause/resume/manipulate a swiffyobject from JS? we can do this with a brute force approach by adding an enter frame event to the flash movie and testing for a change in Flashvars. Below is the Document Class we've been using for our animations. It's worth noting that Swiffy doesn't seem to like AS3 Classes that extend from the same base class, it throws the same cannot redefine property error, so we've duplicated the code from here in the Document class of each of our Flash animations. I've also got a method in there that allows dispatching events from the AS3 to a javascript function called onSwiffyEvent:
package {
import flash.display.MovieClip;
import flash.net.URLRequest;
import flash.net.navigateToURL;
import flash.events.Event;
import flash.utils.setTimeout;
public class BaseAnimation extends MovieClip {
private var _request:URLRequest;
private var _pageName:String;
private var _movieName:String;
public function BaseAnimation() {
_request = new URLRequest();
_pageName = getFlashVar('pageName');
_movieName = getFlashVar('movieName');
addEventListener(Event.ENTER_FRAME, jsListen);
}
private function getFlashVar(name:String):String {
var flashVar:String = stage.loaderInfo.parameters[name] || '';
stage.loaderInfo.parameters[name] = '';
return flashVar;
}
public function dispatchJSEvent(eventName:String, param:String = ''):void {
_request.url = "javascript:onSwiffyEvent('" + eventName + "', '" + param + "', '" + _movieName + "', '" + _pageName + "');";
navigateToURL(_request, "_self");
}
private function jsListen(e:Event):void {
var mode:String = getFlashVar('mode');
if(mode.length) {
switch(mode) {
case 'stop':
stop();
break;
case 'play':
play();
break;
case 'gotoStart':
gotoAndStop('start');
break;
}
}
}
}
}
Swiffy also doesn't seem to like gotoAndStop(0) so I've had to set a frame label of 'start' on the first frame of the animation.
With all this horrible hackery in place, we're able to remove and restart Swiffy animations. The only issue we've found is that detaching and reattaching has caused issues with embedded SVG fonts and we've ended up converting all text to outlines. The above is used like so:
You can call it on the swiffy stage like so:
var stage = new swiffy.Stage(domObject, swiffyObj);
stage.start();
// when reading to remove this element from the DOM do the following:
stage.setFlashVars('mode=gotoStart');
setTimeout(function () {
// timeout is required to ensure that the enterframe listener has time to run
stage.hackPause(true); // paused animation
}, 100);
// you can then remove the containing div from the DOM, but retain it in memory
// after you reattach the div to the DOM, ensuring we've kept hold of our stage variable in memory, you can restart it like this:
stage.hackPause(false); // resumes javascript requestAnimationFrame
stage.setFlashVars('mode=play'); // resumes flash animation
Hope this helps someone, but I also really hope Google start to expose some kind of JS API or ExternalInterface to the Swiffy runtime to give us more control over what is really a pretty great tool.

Try creating a copy of the swiffyobject before passing it to swiffy.Stage(). Swiffy is modifying the object upon instantiation, so with a copy you can simply recreate after destroy()

Related

event.preventDefault is unclear for me even if I understand stopImmediatePropagation and stopPropagation

event.preventDefault seems to be hard to understand to me.(this is the dark side of AS3). :)
stopImmediatePropagation - stopPropagation methods are pretty easy to understand.
Here's a test I made (which need just a copy/paste on the timeline) this may avoid You to spend too much time.
I just do not understand in which case preventDefault may be useful...
If you have some suggestions or links, please could You give me some feedback?
Here is the code :
// This example is coded on the Timeline.
// The Stage must fit 550 * 400 pixels (default size).
import flash.display.DisplayObjectContainer;
import flash.display.MovieClip;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;
import flash.text.TextFormatAlign;
// function to create a MovieClip and return it to a variable.
function createMC(target:DisplayObjectContainer):MovieClip{
var mc:MovieClip = new MovieClip();
target.addChild(mc);
return mc;
}
// function that draws a Rectangle in the MovieClip of your choice.
function dRect(target:MovieClip,x:uint,y:uint,width:uint,height:uint,color:uint,alpha:Number):void{
var g:Graphics = target.graphics;
g.lineStyle(1,0x000000);
g.beginFill(color,alpha);
g.drawRect(x-width/2,y-height/2,width,height);
g.endFill();
}
// event function that trace the properties of the Event.
function traceTarget(me:MouseEvent):void{
trace(" *** event.phase = " + me.eventPhase + ", event.bubbles = " + me.bubbles);
trace("me.target.name = " + me.target.name);
trace("me.currentTarget.name(the listening object) = " + me.currentTarget.name);
/*
Use preventDefault and the events for m1 AND this will be triggered.
MOUSE_DOWN,setText will be called 2 times 1 time for this, the second time for m1!
MOUSE_DOWN,traceTarget will be called 2 times 1 time for this, the second time for m1!
*/
//me.preventDefault();
/*
Use stopPropagation to avoid that the same function is not called if an event occurs.
MOUSE_DOWN,setText will be called !
*/
me.stopPropagation();
/*
Use stopImmediatePropagation to avoid all the functions that another listener may trigger.
MOUSE_DOWN,setText WILL NEVER BE CALLED because traceTarget has been called!
stopImmediatePropagation will only allow the first listener (traceTarget).
*/
//me.stopImmediatePropagation();
trace(me.toString());
// trace the Event that is triggered.
}
function setText(me:MouseEvent):void{
tf_1.text = "me.target.name = " + me.target.name;
tf_1.setTextFormat(tf);
countText++;
trace("setText("+ me.target + ") has been called " + countText + " time(s)");
countText = 0;
}
/*
A counter to see how many times an Event method is triggered
*/
var countText:uint = 0;
/*
Declare a TextField
*/
var tf_1:TextField = new TextField();
this.addChild(tf_1);
tf_1.width = 300;
tf_1.height = 20;
tf_1.x = this.stage.stageWidth - this.stage.stageWidth/2 - tf_1.width/2;
tf_1.y = 30;
var tf:TextFormat = new TextFormat(null,16,0xff0000,true,null,null,null,null,TextFormatAlign.CENTER)
tf_1.text = "Click on a Square/Rectangle";
tf_1.setTextFormat(tf);
/*
Declare 4 MovieClips
3 MovieClips inside each other
- m1 on "this" (root1 in this case).
- m2 inside m1
- m3 inside m2
1 MovieClip on "this" (root1 in this case).
*/
var m1:MovieClip
var m2:MovieClip
var m3:MovieClip
var otherClip:MovieClip
// Create the MovieClips
m1 = createMC(this);
m2 = createMC(m1);
m3 = createMC(m2);
otherClip = createMC(this);
// set the names for the MovieClip's
m1.name = "movieClip_1";
m2.name = "movieClip_2";
m3.name = "movieClip_3";
otherClip.name = "otherClip";
// Draw Rectangles in the MovieClip's
dRect(m1,275,200,100,100,0xff0000,1);
dRect(m2,275,200,50,50,0x00ff00,1);
dRect(m3,275,200,25,25,0x0000ff,1);
dRect(otherClip,100,200,50,50,0xff9900,1);
// Add a listener on m1 MovieClip.
m1.addEventListener(MouseEvent.MOUSE_DOWN,traceTarget,false);
m1.addEventListener(MouseEvent.MOUSE_DOWN,setText,false);
// Add a listener on root (root1 in this case).
this.addEventListener(MouseEvent.MOUSE_DOWN,traceTarget,false);
this.addEventListener(MouseEvent.MOUSE_DOWN,setText,false);
If You could help me to understand the preventDefault method, I should be really happy.
Best regards. Nicolas.
From the documentation of preventDefault()
Many events have associated behaviors that are carried out by default. For example, if a user types a character into a text field, the default behavior is that the character is displayed in the text field. Because the TextEvent.TEXT_INPUT event's default behavior can be canceled, you can use the preventDefault() method to prevent the character from appearing.
Here's an example for that:
package
{
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.display.Sprite;
import flash.events.TextEvent;
public class Main extends Sprite
{
public function Main()
{
var tf:TextField = new TextField();
tf.type = TextFieldType.INPUT;
tf.border = true;
addChild(tf);
tf.addEventListener(TextEvent.TEXT_INPUT, onInput);
}
private function onInput(te:TextEvent):void
{
te.preventDefault();
}
}
}
The TextField is there, but you cannot type into it. If you comment out the line that adds the listener, the default behaviour is not prevent and you can type.
stopImmediatePropagation(), stopPropagation() and eventPhase are all concerned with the event flow: the three phases of capturing, target and bubbling. As such, they can be used to influence if an Event "reaches" an object that added a listener for it. If you want to think about the listeners that you add for an event as custom behaviour, you might call the three above "preventCustom()".
The documentation of the two methods explicitely mentions this:
Note: This method does not cancel the behavior associated with this event; see preventDefault() for that functionality.
preventDefault() deals with the default behaviour associated with the event and has nothing to do with the event flow.
As an example, you might be thinking that you could achieve the same thing as the above example by adding a listener for the capturing phase at stage and then stopping the propagation, like so:
package
{
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.display.Sprite;
import flash.events.TextEvent;
public class Main extends Sprite
{
public function Main()
{
var tf:TextField = new TextField();
tf.type = TextFieldType.INPUT;
tf.border = true;
addChild(tf);
stage.addEventListener(TextEvent.TEXT_INPUT, onInput, true); // new: added to stage and for capturing phase
}
private function onInput(te:TextEvent):void
{
te.stopImmediatePropagation(); // new: stopping propagation
}
}
}
If you execute this code, you will be able to type into the TextField just fine. Again, you're just messing with the event flow here, not the default behaviour. All you do is prevent subsequent listeners to be getting the Event, for example, if you add a listener to the TextField itself as shown below, it will never be executed, because you stop the event flow before it is reached.
package
{
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.display.Sprite;
import flash.events.TextEvent;
public class Main extends Sprite
{
public function Main()
{
var tf:TextField = new TextField();
tf.type = TextFieldType.INPUT;
tf.border = true;
addChild(tf);
tf.addEventListener(TextEvent.TEXT_INPUT, onTfInput);
stage.addEventListener(TextEvent.TEXT_INPUT, onInput, true);
}
private function onTfInput(te:TextEvent):void
{
// never executed
}
private function onInput(te:TextEvent):void
{
te.stopImmediatePropagation();
}
}
}
tl, dr;
If you find a bomb with a self timer ticking, stopImmediatePropagation() will stop other people getting notice about that and might prevent a panic, but only preventDefault() will save your life. If cancelable is false, you better run.
From the as3 docs: preventDefault() "cancels the events behaviour if that behaviour can be cancelled".
One example i can give you is the android back button. If you press the back button, it will minimize your AIR app by default. But if you listen for it and call preventDefault(). It will stop that default behavior so you can navigate to, your last open page instead.
Another example would be the home button on android, you can call preventDefault() but this action cannot he cancelled, so it will be ignored.
A mouse event like your example however, im not in a position to test what exactly that does

Tapestry: Events from Palette component

I'm using palette components on a page and I want the available elements in two of them to change depending on what is selected in the first.
What is the best way to achieve this? Which events are thrown by the palette component, that I could listen to, adapt the palette's model and perform a zone update? I thought it would work the same way as for select components doing something like this:
void onValueChanged() {
// do something
}
Unfortunately that doesn't work for palettes.
I'm using Tapestry 5.4-beta-6, but I guess things haven't changed that much since earlier versions.
I'd probably do this with a mixin.
public class PaletteChange {
#Parameter
private String zone;
#InjectContainer
private Palette palette;
public void afterRender() {
Link eventLink = componentResources.createEventLink("change");
JSONObject args = new JSONOBject(
"id", pallete.getClientId(),
"url", eventLink,
"zone", zone
);
javascriptSupport.addScript("palleteChange(%s)", args);
}
Object onChange(#RequestParameter("value") String value) {
CaptureResultCallback<Object> callback = new CaptureResultCallback<Object>();
resources.triggerEvent("change", new String[] { value }, callback);
return callback.getResult();
}
}
Javascript
function palleteChange(spec) {
var field = $('#' + spec.id + '/select[1]');
field.on('change', function() {
var zoneManager = Tapestry.findZoneManagerForZone(spec.zone);
var params = { value: field.val() };
zoneManager.updateFromURL(spec.url, params);
});
}
Then use the mixin in your code
<t:palette t:id="myPalette" t:mixins="paletteChange" zone="myZone" ... />
<t:zone t:id="myZone">
...
</t:zone>
Page
#Inject
private Zone myZone;
Block onChangeFromMyPalette(String value) {
doStuff(value);
return myZone.getBody();
}
See here for a similar mixin.
I finally used the didChange element together with a similar mixin like the Observe mixin. I put a demo on Github for anyone, who is interested.
Just a few notes:
I used 5.4 beta 6, it already has the necessary client side events.
I couldn't make it work using a Tapestry javascript module, so I still use javascriptSupport.addInitializerCall.
The remaining problem is, that updating the second palette with a zone update will reset any changes the user has made in this palette, since they are only kept on the client side (in a hidden field). I will still need to look into that, but it is not part of the original question.

Haxe application exits when deployed to windows

I am making a game engine in haxe/openfl. so far it's just supposed to display the image that belongs to the thing object. what I have built so far runs perfectly on when deployed as a flash application, but closes instantly when I deploy it as a windows application. It just creates a blank screen in html5. I have not tested other targets. I am using HIDE, and every time it crashes, HIDE brings up the message: "File c:\Users\Adu\Documents\HaxeProjects\Downloaded\Export\windows\cpp\bin\Downloaded.exe was changed. Reload?" and gives me the options yes or no. My answer doesn't seem to change the situation. when I manually go into the export directory and run the application, it gives the error: "Error Custom([file_write,stderr]). Here is my code:
Main:
package;
import openfl.display.Graphics;
import openfl.Assets;
import openfl.display.Bitmap;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.Lib;
import openfl.text.TextField;
import openfl.text.TextFormat;
import openfl.ui.Keyboard;
import openfl.events.*;
class Main
{
static var obj(default,default):ObjectManager; //contains the list that all gameobjects add themselves to
static var stage = Lib.current.stage;
public static function main() // this is the gameloop
{
// static entry point
startUp();
var running = true; // gives me a way to exit the gameloop
while (running)
{
logic();
render();
Lib.current.stage.addEventListener(KeyboardEvent.KEY_DOWN, function(event)
{
if (event.keyCode == Keyboard.ESCAPE)
{
running=false;
}
});
}
}
static function startUp() // runs once, when the game is started
{
obj= new ObjectManager();
stage.align = openfl.display.StageAlign.TOP_LEFT;
stage.scaleMode = openfl.display.StageScaleMode.NO_SCALE;
}
static function logic() // loops, this handles the logic
{
var thing = new GameObject("assets/pixel_thing.png", 1, obj);
var mech = new GameObject("assets/mechwarrior.png", 0, obj);
}
static function render() // runs right after logic and draws everything to the screen
{
for (i in obj.objects) //iterates through a list of gabeobjects and draws them, it is 2 dimensional so that I can draw objects in blocks
{
for (j in i)
{
Lib.current.addChild(j);
}
}
}
}
GameObject:
package ;
import openfl.display.BitmapData;
import openfl.Assets;
import openfl.display.Bitmap;
import openfl.display.Sprite;
import openfl.events.Event;
import openfl.Lib;
import openfl.text.TextField;
import openfl.text.TextFormat;
class GameObject extends Sprite
{
public function new(?image:String, zOrder:Int, objectManager:ObjectManager) // image is the image, zorder is which layer it's drawn on, lower layers are drawn on top objectmanager is just there to help me pass the list to the object
{
super();
var data = Assets.getBitmapData(image);//this is the image data
var bitmap:Bitmap = new Bitmap(data);//this is the actual image
Lib.current.stage.addChild(bitmap);//this sraws the image when the object is instantiated
objectManager.objects[zOrder].push(this);// this adds it to the list of objects
}
}
ObjectManager:
package ;
class ObjectManager
{
public var objects = new Array<Array<GameObject>>();
}
why is it that it works on flash but not windows? How do I fix this?
First off - this doesn't work fine on flash either. Are you running this in the flash debug player? If you don't, which I assume is the case, you won't see any exceptions.
There's a null reference error at this line:
objectManager.objects[zOrder].push(this);// this adds it to the list of objects
You are accessing the array at index zOrder, which doesn't exist. objects is being initialized to [], which does not include the "inner arrays" (it can't, really, how would it know how many of them there should be?).
Now, Windows builds don't give you very helpful debug information by default. A simple way around is to use neko (which mostly behaves the same as hxcpp builds, except it compiles faster and performs worse) for debugging, where you get a stacktrace by default on crashes.
Sure enough, it's the same issue as in flash, the only difference is that native builds crash while flash just "ignores it" and tries to carry on.
Invalid field access : objects
Called from GameObject::new line 92
Called from GameObject::$init line 83
Called from Main::logic line 61
Called from Main::main line 38
Called from Reflect::callMethod line 58
Called from ApplicationMain::main line 91
Called from openfl.Lib::create line 113
For better hxcpp build debug info, you might want to have a look at the crashdumper lib.

URLLoader Event.Complete Not Triggering

private var csv:URLLoader = new URLLoader();
private var array:Array = new Array();
private var urlr:URLRequest = new URLRequest();
public function loadRecipe(path:String):void
{
try
{
csv.dataFormat = URLLoaderDataFormat.TEXT;
urlr = new URLRequest(path);
csv.addEventListener(Event.COMPLETE, finishRecipe);
csv.load(urlr);
}
catch (e:SecurityErrorEvent)
{
trace("1");
}
catch (e:IOErrorEvent)
{
trace("2");
}
}
public function finishRecipe(e:Event):void
{
var csvString:String = csv.data as String;
array = csvString.split(",");
}
My code that I'm working with is above. I can't get the completion event to ever trigger, that is, my array is never populated. Can anyone give me insight as to why?
EDIT:
I changed to get rid of all the weak references and check for errors. I don't get any errors.
I've run into this bug frequently over the years. When certain browsers (FireFox, Chrome) retrieve the file from cache instead of network, the loader will dispatch a PROGRESS event but never COMPLETE.
Try clearing your browser cache and see whether the file loads correctly next time. If so, you can do one of two things as a workaround:
Break the cache by adding a random string to the end of your request URL.
urlr = new URLRequest(path + "?cachebust=" + Math.floor(100000+900000*Math.random()));
This is simple to code, but has obvious disadvantages, forcing unnecessary reloads.
Listen for both COMPLETE and PROGRESS events. In the PROGRESS handler, check to see if bytesLoaded matches bytesTotal. If it does, remove all handlers and continue as if it were a COMPLETE event.
... somewhere in your code ...
loader.addEventListener(Event.COMPLETE, handleComplete);
loader.addEventListener(ProgressEvent.PROGRESS, handleProgress);
... somewhere else
private function handleProgress(evt:ProgressEvent):void
{
checkLoadComplete();
}
private function handleComplete(evt:Event):void
{
checkLoadComplete();
}
private function checkLoadComplete():void
{
if(loader.bytesTotal > 0 && loader.bytesLoaded == loader.bytesTotal) {
loader.removeEventListener(Event.COMPLETE, handleComplete);
loader.removeEventListener(ProgressEvent.PROGRESS, handleProgress);
... your code here
}
}
Ummm just looking at the code you provided it seems like you actually try to catch the errors using try/catch. In order to find errors, you have to start listening to them on the actual loader. Like this:
public function Foobar() {
var loader:URLLoader;
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
loader.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
loader.addEventListener(ProgressEvent.PROGRESS, onProgress);
loader.addEventListener(Event.COMPLETE, onComplete);
}
private function onComplete(e:Event):void {}
private function onProgress(e:ProgressEvent):void {}
private function onSecurityError(e:SecurityErrorEvent):void {}
private function onIOError(e:IOErrorEvent):void {}

Asynchronous image loading in AS3

I understand that images are to be loaded asynchronously in AS3, and that that synchronisation should be handled using events and event listeners.
So, in a simple case, it would look like this:
var loader : Loader = new Loader();
var im_file: URLRequest = new URLRequest ("imfile.png");
loader.load(im_file);
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loading_complete);
function loading_complete (e : Event) : void
{ // ... do smt with your loaded data // }
What I want to do is have a PreLoader class that will load all the images I need beforehand.
In that case, how do I let all the other classes know when the loading is done?
Do I dispatch events? What is the best practise in this case?
Thanks in advance,
Praskaton
Most likely you want to create a queue and add your image paths to the queue. Then after each image is done loading, you proceed to the next item in the queue. When all images are loaded, you dispatch a COMPLETE event or something similar to let your app know it's all done.
Check QueueLoader or Casalib for how they implement single or bulk image loading.
Adding to the answer that #Boon provided, this is how you could go about the actual setting up of the image queue.
Firstly, you need a list that will store all of the images that still need to be loaded. This makes it easy for you to define as many images as you want. It can be the 'queue':
var queue:Array = [
"http://interfacelift.com/wallpaper/previews/03177_orionnebulaintheinfrared#2x.jpg",
"http://interfacelift.com/wallpaper/previews/03175_purpleclouds#2x.jpg",
"http://interfacelift.com/wallpaper/previews/03173_goodmorning2013#2x.jpg"
];
The next thing to do is set up what I would call the 'core' method of what we're doing. It will handle loading the next image as well as notifying us when the queue is empty. It looks something like this:
function loadNext():void
{
if(queue.length > 0)
{
// Notice here that we use .pop() on the queue, which will select and
// remove the last item from queue.
var req:URLRequest = new URLRequest( queue.pop() );
var photo:Loader = new Loader();
photo.load(req);
photo.contentLoaderInfo.addEventListener(Event.COMPLETE, loadComplete);
}
else
{
// The queue is finished - dispatch an event or whatever you fancy to
// let the rest of the application know we're done here.
trace("Queue finished.");
}
}
And then of course our listener function to deal with the completion of loaded images. Notice here that we call loadNext() - this is the key to beginning the load of the next image in the queue only once the currently loading image has finished.
function loadComplete(e:Event):void
{
addChild(e.target.content as Bitmap);
// Begin loading next image in the queue.
loadNext();
}
And to start the process we of course just use this, which will either immediately notify us that the queue is finished if it's empty, or start loading the images in sequence.
// Start loading the queue.
loadNext();
Additional / tidy up:
If you want to be able to recycle this code or just tidy up, you can easily make this into a class. The class could be called ImageQueue and its structure will contain the above queue array, loadNext() method and loadComplete() method. It can also have an add() method for adding images to the queue initially in a cleaner manner.
Here is the foundation of that class, which you can finish up if you're interested:
public class ImageQueue
{
private var _queue:Array = [];
public function add(image:String):void{ }
public function loadNext():void{ }
private function _loadComplete(e:Event):void{ }
}

Resources