In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: ClassLoader

load classes from other codes in wonderfl
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/
loading external two swfs as runtime classes
swf#draws blue rectangle: http://wonderfl.net/code/c30b2c2f831457fb94c38976e15c7acbd7ed9100
swf#draws red rectangle : http://wonderfl.net/code/b5c2996d0f2573202a541a9190ae90c4002b076b
take good care of ApplicationDomain settings
when you loading multiple classes of the same name
Get Adobe Flash player
by 9re 02 Oct 2009
// forked from mash's ClassLoader
// load classes from other codes in wonderfl
// 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/
// loading external two swfs as runtime classes
// swf#draws blue rectangle: http://wonderfl.net/code/c30b2c2f831457fb94c38976e15c7acbd7ed9100
// swf#draws red rectangle : http://wonderfl.net/code/b5c2996d0f2573202a541a9190ae90c4002b076b
// take good care of ApplicationDomain settings
// when you loading multiple classes of the same name
package  
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.geom.Rectangle;
    import flash.system.ApplicationDomain;
    import flash.system.LoaderContext;
    import flash.text.TextField;
    
    import Box2D.Common.Math.b2Vec2;
    
    public class ApplicationDomain_Sample_CustomDataStructure extends Sprite
    {
        private var _urls:Array = ["http://swf.wonderfl.net/swf/usercode/c/c3/c30b/c30b2c2f831457fb94c38976e15c7acbd7ed9100.swf", "http://swf.wonderfl.net/swf/usercode/b/b5/b5c2/b5c2996d0f2573202a541a9190ae90c4002b076b.swf"];
        private var _runtimeClasses:Array = [];
        
        public function ApplicationDomain_Sample_CustomDataStructure() 
        {
            loadClasses(_urls.slice(), "Runtime_Sample_CustomDataStructure", _runtimeClasses, onComplete);
        }
        
        private function loadClasses($loadPaths:Array, $className:String, $classStorage:Array, $onComplete:Function, $onCompleteParams:Array = null):void {
            var ldr:ClassLoader = new ClassLoader();
            ldr.addEventListener(ClassLoader.CLASS_LOADED, function(e:Event):void {
                var c:Class = ldr.getClass($className);
                $classStorage.push(c);
                
                if ($loadPaths.length > 0)
                    loadClasses($loadPaths, $className, $classStorage, $onComplete, $onCompleteParams);
                else
                    $onComplete.apply(null, $onCompleteParams);
            }, false, 0, true);
            try {
                var context:LoaderContext = new LoaderContext();
                context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);;
                ldr.load($loadPaths.shift(), context);
            } catch (e:Error) {
                //
            }
        }
        
        private function onComplete():void
        {
            var bd:BitmapData = new BitmapData(stage.stageWidth, stage.stageHeight);
            addChild(new Bitmap(bd));
            
            var runtimeInstance:Object;
            for (var i:int = 0; i < _runtimeClasses.length; ++i) {
                runtimeInstance = new _runtimeClasses[i]();
                runtimeInstance.drawBitmap(bd, runtimeInstance.getRectangle(), runtimeInstance.getColor());
            }
            
            var vec:b2Vec2 = runtimeInstance.getVector();
            var tf:TextField = new TextField();
            tf.text = vec.x + ", " + vec.y;
            addChild(tf);
        }
        
    }
}

import flash.display.Loader;
import flash.display.Sprite;
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;
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.LoaderContext;

class ClassLoader extends Sprite {
    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, context:LoaderContext):void {
        swfLib = lib;
        request = new URLRequest(swfLib);
        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));
    }
}