PDA

View Full Version : Loading mutliple files



superpeppe
March 24th, 2008, 12:59 PM
Hi

Can anyone tell me how to load multiple files using the same loader object? Im having a quite strange problem which is very hard to explain:td:

Thanks in advance

Felixz
March 24th, 2008, 01:12 PM
I'm not sure but maybe
completeHandler(event:Event):void {
anotherHolder.addChild(event.target.content);
event.target.loader.load(new URLRequest());
}But loading next file on same loader unloads old one, so I don't know it will be flawless

superpeppe
March 24th, 2008, 01:25 PM
Thanks for the quick reply, but it's not quite what im looking for, in the end i want an array which contains all the loaded files.

My problem when I wrote it, was that all the array "elements/indexes" pointed at the latest loaded file.

Felixz
March 24th, 2008, 06:45 PM
U used that or something else?
completeHandler(event:Event):void {
array.push(event.target.content);
event.target.loader.load(new URLRequest());
}

trundrumbalind
March 24th, 2008, 06:46 PM
Man, check this out. It's INCREDIBLE! It can load a queue of items and show information (progress etc) of each item and even the queue as a whole. Wicked stuff...
http://blog.hydrotik.com/2007/09/14/queueloader-as3-rev-5/

superpeppe
March 24th, 2008, 07:30 PM
Thanks for the link trundrumbalind, but it really is way to advanced.

The class I wrote looks like this:


package com.sitoon.loader {
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;

public class SiLoader {
private var _completeFunc:Function, _errorFunc:Function;
private var _loader:Loader;
private var _URLList:Array, _dirList:Array;
private var _nowLoading:int, _dirLoading:int;

public function SiLoader() {
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.C OMPLETE, loadStep);
_loader.contentLoaderInfo.addEventListener(IOError Event.IO_ERROR, ioErrorHandler);
}
public function load(URLList:Array, dirList:Array, completeFunc:Function, errorFunc:Function = null):void {
_URLList = URLList;
_dirList = dirList;
_completeFunc = completeFunc;
_errorFunc = errorFunc;
_nowLoading = _dirLoading = 0;
loadStep();
}
private function loadStep(event:Event = null):void {
if (event) {
_URLList[_nowLoading] = event.target;
_nowLoading++;
_dirLoading = 0;
}
if (_nowLoading < _URLList.length) {
//This is very very strange but neccessary? :[
//START OF THE STRANGE
_loader.contentLoaderInfo.removeEventListener(Even t.COMPLETE, loadStep);
_loader.contentLoaderInfo.removeEventListener(IOEr rorEvent.IO_ERROR, ioErrorHandler);
_loader = new Loader();
_loader.contentLoaderInfo.addEventListener(Event.C OMPLETE, loadStep);
_loader.contentLoaderInfo.addEventListener(IOError Event.IO_ERROR, ioErrorHandler);
//END OF THE STRANGE

var request:URLRequest = new URLRequest(_URLList[_nowLoading]);
_loader.load(request);
} else {
_completeFunc.call();
}
}
private function ioErrorHandler(event:IOErrorEvent):void {
if (++_dirLoading < _dirList.length) {
loadStep();
trace(_dirList[_dirLoading - 1] + "/" + _URLList[_nowLoading] + " could not be loaded, trying: " + _dirList[_dirLoading] + "/" + _URLList[_nowLoading]);
} else {
if (_errorFunc != null) {
_errorFunc.call();
}
trace(_URLList[_nowLoading] + " could not be loaded from: " + _dirList.toString());
}
}

public function get filesLoaded():int { return _nowLoading }
public function get filesTotal():int { return _URLList.length }
public function get currentFileLoaderInfo():LoaderInfo { return _URLList[_nowLoading].loaderInfo }

public function get ContentList():Array {
if (_nowLoading >= _URLList.length) {
return _URLList;
}
throw Error("Loading not finished");
}
}
}

If you remove the 5 lines inside "START OF THE STRANGE" to "END OF THE STRANGE". You will notice that all the elements in ContentList or URLList is the same, can anyone please come up with a solution to this?

By the way the dirList is made so that you can load from the same folder "/" if that does not work you can then load from "otherFolder/" or even "thirdFolder/":thumb2:

Felixz
March 24th, 2008, 07:55 PM
event.target points at contentLoaderInfo.
package com.sitoon.loader {
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
public class SiLoader extends Loader {
private var _completeFunc:Function, _errorFunc:Function;
private var _URLList:Array, _dirList:Array,loaded:Array=new Array();
private var _nowLoading:int=-1, _dirLoading:int=0;
public function SiLoader() {
contentLoaderInfo.addEventListener(Event.COMPLETE, loadStep);
contentLoaderInfo.addEventListener(IOErrorEvent.IO _ERROR, ioErrorHandler);
}
public function load(URLList:Array, dirList:Array, completeFunc:Function, errorFunc:Function = null):void {
_URLList = URLList;
_dirList = dirList;
_completeFunc = completeFunc;
_errorFunc = errorFunc;
loadStep();
}
private function loadStep(event:Event = null):void {
if (event) loaded.push(content);
if (++_nowLoading < _URLList.length) {
load(new URLRequest(_URLList[_nowLoading]));
} else {
_completeFunc.call();
}
}
private function ioErrorHandler(event:IOErrorEvent):void {
if (++_dirLoading < _dirList.length) {
loadStep();
trace(_dirList[_dirLoading - 1] + "/" + _URLList[_nowLoading] + " could not be loaded, trying: " + _dirList[_dirLoading] + "/" + _URLList[_nowLoading]);
} else {
if (_errorFunc != null) {
_errorFunc.call();
}
trace(_URLList[_nowLoading] + " could not be loaded from: " + _dirList.toString());
}
}
public function get filesLoaded():int { return _nowLoading }
public function get filesTotal():int { return _URLList.length }
public function get currentFileLoaderInfo():LoaderInfo { return contentLoaderInfo }
public function get ContentList():Array {
return loaded;
}
}Here u can look at some improvements and we can work together to improve mine loader to fit even ur needs.
http://www.kirupa.com/forum/showthread.php?t=289142&highlight=Class+Loader

superpeppe
March 25th, 2008, 06:29 AM
Well, I have tried something similar, and you example is great, but then comes a completely different problem:

When I have made a SWF (in the flash IDE) with a MovieClip called MyMovie, set for exporting for actionscript with the class name "MyMovie", how do I get access to it?

I tried the contentList[0].applicationDomain.getDefinition("MyMovie"), but that does not work with the _loader.content only with the event.target???

Felixz
March 25th, 2008, 08:15 AM
in such case u could use mine class and just call in main fla new MyMovie()