[LEFT]Hey everyone!
I made a custom event, but was getting this runtime error:
TypeError: Error #1034: Type Coercion failed: cannot convert flash.events::Event@1498f491 to com.esidegallery.net.BatchLoaderEvent.
and eventually found what I thought was the the answer here - http://livedocs.adobe.com/flex/2/docs/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001644.html
I made the necessary changes, but I’m still getting the same error!
It’s just a batch loader that dispatches a custom event for every item that loads, with its LoaderInfo object attached. Here’s the event code:
package com.esidegallery.net
{
import flash.events.Event;
import flash.display.LoaderInfo;
public class BatchLoaderEvent extends Event
{
public static const COMPLETE:String = "complete";
public static const ALL_COMPLETE:String = "allComplete";
private var _loaderInfo:LoaderInfo;
public function BatchLoaderEvent(type:String, loaderInfo:LoaderInfo = null)
{
super(type);
_loaderInfo = loaderInfo;
}
override public function clone():Event
{
return new BatchLoaderEvent(type, _loaderInfo);
}
public function get loaderInfo():LoaderInfo
{
return _loaderInfo;
}
}
}
And just incase, here’s the class that dispatches it…
package com.esidegallery.net
{
import flash.events.EventDispatcher;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.events.HTTPStatusEvent;
import flash.events.ProgressEvent;
import flash.net.URLRequest;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.system.LoaderContext;
/**
* Notes: Needs code to handle HTTP request time-outs or something. Internal timeout code.
**/
public class BatchLoader extends EventDispatcher
{
private var _context:LoaderContext;
private var _urlArray:Array;
private var _maxConcurrentLoads:int;
private var _loaderArray:Array = new Array();
private var _nextLoadIndex:int;
public function BatchLoader(urlArray:Array, maxConcurrentLoads:int = 1):void
{
_urlArray = urlArray;
_maxConcurrentLoads = maxConcurrentLoads;
// Forces crossdomain.xml checking.
_context = new LoaderContext();
_context.checkPolicyFile = true;
for (var i:* in urlArray)
{
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(BatchLoaderEvent.COMPLETE, onLoadComplete);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR , onLoadError);
loader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, onLoadProgress);
loader.contentLoaderInfo.addEventListener(HTTPStatusEvent.HTTP_STATUS, onHTTPStatus);
_loaderArray.push(loader);
}
}
public function start():void
{
_nextLoadIndex
for (var i:int; i < _maxConcurrentLoads; i ++)
{
_loaderArray*.load(new URLRequest(_urlArray*), _context);
}
}
private function onLoadComplete(e:BatchLoaderEvent):void
{
trace("Loaded", e.target.url);
dispatchEvent(new BatchLoaderEvent(BatchLoaderEvent.COMPLETE, LoaderInfo(e.target)));
if (_nextLoadIndex < _loaderArray.length)
{
_loaderArray[_nextLoadIndex].load(new URLRequest(_urlArray[_nextLoadIndex]), _context);
_nextLoadIndex ++;
}
else dispatchEvent(new BatchLoaderEvent(BatchLoaderEvent.ALL_COMPLETE));
}
private function onLoadError(e:IOErrorEvent)
{
trace("Failed to load", e.target.url);
if (_nextLoadIndex < _loaderArray.length)
{
_loaderArray[_nextLoadIndex].load(new URLRequest(_urlArray[_nextLoadIndex]), _context);
_nextLoadIndex ++;
}
}
private function onLoadProgress(e:Event)
{
// Needs custom ContentLoaderProgressEvent
}
private function onHTTPStatus(e:HTTPStatusEvent)
{
//trace(e.status);
}
}
}
Am I missing something?
[/LEFT]