Pages

Google+

Friday, November 27, 2009

My Latest Implementaion of Skipping AS3 Events

In a previous post, I talked about how I use method closures to avoid AS3 events. Since then I have been using the technique extensively and I still don't use custom events - at all. The previous example was OK, but this one is better. This is also a class I use in most my projects.



I still haven't decided if I want to continue to push the data back into the callback function, or if the object's interface should include an accessor for the data. I kinda like pushing the data because it will force a compile error. If I included it in the interface, then I'd have to make the accessor return a wild card, '*', to make the interface interchangeable - since I use "Disposable_Object" often...I suppose I could have it implement 2 interfaces...that may be overkill however.

Implementation:
private function load_skins() : void
{
external_skin_loader = new External_Swf_Skin_Loader('my_url', create_view);
}

private function create_view(skins : ApplicationDomain) : void
{
view = new View(skins);
}

...and now the classes:
package
{
public interface Disposable_Object
{
function dispose() : void;
}
}

package
{
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;

public class External_Swf_Skin_Loader implements Disposable_Object
{
public var execute_callback : Function;
private var swf_loader : Loader;
private var swf_domain : ApplicationDomain;

public function External_Swf_Skin_Loader(skinURL : String,skins_loaded_callback : Function)
{
this.execute_callback = skins_loaded_callback;
load_skin_swf(skinURL);
}

private function load_skin_swf(skinURL : String) : void
{
swf_loader = new Loader();
swf_loader.contentLoaderInfo.addEventListener(Event.COMPLETE, get_application_domain);
swf_loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, load_error);
swf_loader.load(new URLRequest(skinURL));
}

private function get_application_domain(event : Event) : void
{
swf_domain = ApplicationDomain(swf_loader.contentLoaderInfo.applicationDomain);
execute_callback(swf_domain);
}

private function load_error(event : IOErrorEvent) : void
{
// implemented only in case of an error to prevent runtime error
}

public function dispose() : void
{
swf_loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, get_application_domain);
swf_loader.contentLoaderInfo.removeEventListener(IOErrorEvent.IO_ERROR, load_error);
execute_callback = null;
swf_loader = null;
swf_domain = null;
}
}
}