The following is a piece of as3 code which loads a class library at
runtime. This example was built from an example in the AS3 docs for
Class.

It works by defining a "Library" class which declares some public vars
which are bound to classes. When the main app loads this library swf,
it has access to the library object via the loader's 'content' field,
and from that it can grab the class bindings.  This example works for
me, so
I am thinking of implementing the <import> feature using this. All of
the classes which are defined inside of an <import> will be listed
in a library class, and when loaded, pointers to the classes will get
copied to the lz dictionary.

Resources can be embedded the same way, but will be copied to the main
app resource tables.



Main.as:

package {
  import flash.display.*;
  import flash.events.*;
  import flash.net.*;
  import flash.utils.*;
  import flash.system.*;

    public class Main extends Sprite {

        public var libname:String = "MyLibrary.swf";
        public var loader:Loader = null;
        public var lz:Object = {};


        public function Main () {

            // Load the library named "MyLibrary.swf" at runtime
            var request:URLRequest = new URLRequest(libname);
            request.method = URLRequestMethod.GET;
            this.loader = new Loader();
            var info:LoaderInfo = loader.contentLoaderInfo;
            info.addEventListener(Event.COMPLETE, addNew);
            this.loader.load(request);
        }


        public function addNew(event:Event):void {
            var library:Object = event.target.content;
            // grab the class bindings we defined out of it
            lz.circle = library.circleCelass;
            var circle:Shape = new lz.circle(0xffccaa, 20);
            addChild(circle);
        }
    }
}



MyLibrary.as:
     package {
      import flash.display.Sprite;
      public class MyLibrary extends Sprite {

          public var circleClass:Class = Circle;
          public var clickyClass:Class = Clicky;
          public function MyLibrary() {
          }
      }
     }

     import flash.display.Shape;
     class Circle extends Shape {
      public function Circle(color:uint = 0xFFCC00, radius:Number = 10) {
          graphics.beginFill(color);
          graphics.drawCircle(radius, radius, radius);
      }
     }





-- 
Henry Minsky
Software Architect
[EMAIL PROTECTED]

Reply via email to