load classes from other codes in wonderfl
http://wonderfl.net/code/585fe2c7e3ac3f0c2927a33e3b7a71ac8429ea3f
http://livedocs.adobe.com/flex/3_jp/langref/flash/system/ApplicationDomain.html
according to livedocs license: http://creativecommons.org/licenses/by-nc-sa/3.0/
// load classes from other codes in wonderfl
// http://wonderfl.net/code/585fe2c7e3ac3f0c2927a33e3b7a71ac8429ea3f
// http://livedocs.adobe.com/flex/3_jp/langref/flash/system/ApplicationDomain.html
// according to livedocs license: http://creativecommons.org/licenses/by-nc-sa/3.0/
package {
import flash.display.*;
import flash.errors.IllegalOperationError;
import flash.events.Event;
import flash.text.TextField;
public class ApplicationDomainExample extends Sprite {
private var loader:ClassLoader;
public function ApplicationDomainExample() {
loader = new ClassLoader();
loader.addEventListener(ClassLoader.LOAD_ERROR, loadErrorHandler);
loader.addEventListener(ClassLoader.CLASS_LOADED, classLoadedHandler);
loader.load("http://swf.wonderfl.net/swf/usercode/5/58/585f/585fe2c7e3ac3f0c2927a33e3b7a71ac8429ea3f.swf");
}
private function loadErrorHandler(e:Event):void {
throw new IllegalOperationError("Cannot load the specified file.");
}
private function classLoadedHandler(e:Event):void {
var greeterClassRef:Class = loader.getClass("RuntimeClasses");
var greeter:Object = new greeterClassRef();
trace(greeter.greet()); // Hello World
var tf:TextField = new TextField;
tf.text = greeter.greet();
addChild( tf );
var bd :BitmapData = greeter.square();
var bmp :Bitmap = new Bitmap( bd );
bmp.x = 100;
bmp.y = 100;
addChild( bmp );
}
}
}
import flash.display.Loader;
import flash.errors.IllegalOperationError;
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.IOErrorEvent;
import flash.events.SecurityErrorEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;
class ClassLoader extends EventDispatcher {
public static var CLASS_LOADED:String = "classLoaded";
public static var LOAD_ERROR:String = "loadError";
private var loader:Loader;
private var swfLib:String;
private var request:URLRequest;
private var loadedClass:Class;
public function ClassLoader() {
loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, completeHandler);
loader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, ioErrorHandler);
loader.contentLoaderInfo.addEventListener(SecurityErrorEvent.SECURITY_ERROR, securityErrorHandler);
}
public function load(lib:String):void {
swfLib = lib;
request = new URLRequest(swfLib);
var context:LoaderContext = new LoaderContext();
context.applicationDomain = ApplicationDomain.currentDomain;
loader.load(request, context);
}
public function getClass(className:String):Class {
try {
return loader.contentLoaderInfo.applicationDomain.getDefinition(className) as Class;
}
catch(e:Error) {
throw new IllegalOperationError(className + " definition not found in " + swfLib);
}
return null;
}
private function completeHandler(e:Event):void {
dispatchEvent(new Event(ClassLoader.CLASS_LOADED));
}
private function ioErrorHandler(e:Event):void {
dispatchEvent(new Event(ClassLoader.LOAD_ERROR));
}
private function securityErrorHandler(e:Event):void {
dispatchEvent(new Event(ClassLoader.LOAD_ERROR));
}
}