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: Loading external classes using ApplicationDomain

This is the standard example for importing external classes 
* from an ApplicationDomain class, but it won't work in wonderfl.
*
* Is there a way to make this work, or is it just impossible to do?
*
* 
* No, it's not impossible.
* 
* If you load the SWF file A from different domain samplerinfo.com
* the class Bounce difined in Ais loaded into current application domain as
* samplerinfo.com::Bounce@??????
* and you cannot find the class Bounce.
* 
* if you want to load the classes of A directly into current application domain,
* set securityDomain property of loader context to SecurityDomain.currentDomain
* 
* cheers,
* happy fun coding!!
Get Adobe Flash player
by 9re 15 Jan 2010
    Embed
/**
 * Copyright 9re ( http://wonderfl.net/user/9re )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/uab8
 */

// forked from Arkatufus's Loading external classes using ApplicationDomain
/*
 *
 * This is the standard example for importing external classes 
 * from an ApplicationDomain class, but it won't work in wonderfl.
 *
 * Is there a way to make this work, or is it just impossible to do?
 *
 * 
 * No, it's not impossible.
 * 
 * If you load the SWF file A from different domain samplerinfo.com
 * the class Bounce difined in Ais loaded into current application domain as
 * samplerinfo.com::Bounce@??????
 * and you cannot find the class Bounce.
 * 
 * if you want to load the classes of A directly into current application domain,
 * set securityDomain property of loader context to SecurityDomain.currentDomain
 * 
 * cheers,
 * happy fun coding!!
 */
package {
	import flash.system.SecurityDomain;
    import flash.display.DisplayObject;
    import flash.display.Sprite;
	import flash.display.MovieClip;
    import flash.errors.IllegalOperationError;
    import flash.events.Event;

    [SWF(backgroundColor="#FFFFFF", frameRate=60)]
    public class BounceLoader extends Sprite {
        private var loader:ClassLoader;

        public function BounceLoader() {
            loader = new ClassLoader();
            loader.addEventListener(ClassLoader.LOAD_ERROR,loadErrorHandler);
            loader.addEventListener(ClassLoader.CLASS_LOADED,classLoadedHandler);
            loader.load("http://samplerinfo.com/wonderfl/Bounce.swf");
        }

        private function loadErrorHandler(e:Event):void {
            trace("Cannot load the specified file.");
        }

        private function classLoadedHandler(e:Event):void {
            var Bounce:Class = loader.getClass("Bounce");
            var bounce:MovieClip;
            
            for(var i:int=0; i<10; i++){
                bounce = new Bounce();
                bounce.x = Math.random() * 200;
                bounce.y = Math.random() * 200;
                addChild(bounce);
            }
		}
    }
}

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;
import flash.system.SecurityDomain;

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;
        context.securityDomain = SecurityDomain.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));
    }
}