{"id":30,"date":"2005-11-04T14:28:47","date_gmt":"2005-11-04T19:28:47","guid":{"rendered":"http:\/\/wahlers.com.br\/claus\/blog\/as3-loading-class-libraries-at-runtime\/"},"modified":"2006-05-22T12:22:14","modified_gmt":"2006-05-22T17:22:14","slug":"as3-loading-class-libraries-at-runtime","status":"publish","type":"post","link":"http:\/\/wahlers.com.br\/claus\/blog\/as3-loading-class-libraries-at-runtime\/","title":{"rendered":"AS3: Loading Class Libraries at Runtime"},"content":{"rendered":"<p>The Flash Player 8.5 features a nifty API to dynamically load assets at runtime, using the <code>flash.display.Loader<\/code> and <code>flash.system.ApplicationDomain<\/code> classes. Let&#8217;s assume we have a package, <code>test.c1<\/code>, that we want to keep in a separate SWF and only load it into the main SWF when needed.<\/p>\n<p>We create a project Main that has the following structure:<\/p>\n<ul>\n<li><code>Main.as<\/code><\/li>\n<li><code>Child1.as<\/code><\/li>\n<li><code>test\/c1\/TestClass.as<\/code><\/li>\n<\/ul>\n<p><code>Child1.as<\/code> is compiled into <code>Child1.swf<\/code> and contains <code>test.c1.TestClass<\/code>.<\/p>\n<p><code>Main.as<\/code> is our main application class that loads <code>Child1.swf<\/code> and uses <code>test.c1.TestClass<\/code>.<\/p>\n<p><strong>test\/c1\/TestClass.as<\/strong><\/p>\n<blockquote>\n<pre>\r\npackage test.c1\r\n{\r\n   public class TestClass\r\n   {\r\n      public function TestClass() { }\r\n\r\n      public function method():String {\r\n         return 'test.c1.TestClass.method()';\r\n      }\r\n   }\r\n}<\/pre>\n<\/blockquote>\n<p><strong>Child1.as<\/strong><\/p>\n<blockquote>\n<pre>\r\npackage\r\n{\r\n   import flash.display.Sprite;\r\n   import test.c1.TestClass;\r\n\r\n   public class Child1 extends Sprite\r\n   {\r\n      public var TestClassInst:TestClass;\r\n\r\n      public function Child1() { }\r\n   }\r\n}\r\n<\/pre>\n<\/blockquote>\n<p><strong>Main.as<\/strong><\/p>\n<blockquote>\n<pre>\r\npackage\r\n{\r\n   import flash.display.Sprite;\r\n   import flash.display.Loader;\r\n   import flash.system.ApplicationDomain;\r\n   import flash.net.URLRequest;\r\n   import flash.events.*;\r\n   import flash.util.trace;\r\n\r\n   public class Main extends Sprite\r\n   {\r\n      private var child1:Loader;\r\n\t\t\r\n      public function Main()\r\n      {\r\n         var url1:URLRequest = new URLRequest('Child1.swf');\r\n         url1.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);\r\n\r\n         child1 = new Loader();\r\n         child1.addEventListener(EventType.COMPLETE, onChild1Complete);\r\n         child1.load(url1);\r\n      }\r\n\r\n      private function onChild1Complete(e:Event):Void {\r\n         trace('onChild1Complete ' + e);\r\n         var c:Class = child1.loadeeInfo.applicationDomain.getClass('test.c1.TestClass');\r\n         var co:Object = new c();\r\n         trace('test.c1.TestClass.method() returns: ' + co.method());\r\n      }\r\n   }\r\n}\r\n<\/pre>\n<\/blockquote>\n<p>Using this method we could have different implementations of an API in different SWFs, and load the one we need at runtime. Naturally, we would want to use interfaces to ensure the API implements the right things, and use those interfaces for both the external modules and the main app. Note that i instantiate <code>test.c1.TestClass<\/code> as an anonymous object in the above example (<code>var co:Object = new c();<\/code>). I actually should have provided an interface definition <code>test.ITestClass<\/code> that describes the features of that external class (<code>var co:ITestClass = new c();<\/code>), however the alpha of Flex Builder has a bug that currently prevents us from doing so. Macromedia is aware of that bug so hopefully this is fixed soon.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The Flash Player 8.5 features a nifty API to dynamically load assets at runtime, using the <code>flash.display.Loader<\/code> and <code>flash.system.ApplicationDomain<\/code> classes. Let&#8217;s assume we have a package, <code>test.c1<\/code>, that we want to keep in a separate SWF and only load it into the main SWF when needed. <a href=\"http:\/\/wahlers.com.br\/claus\/blog\/as3-loading-class-libraries-at-runtime\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,4],"tags":[],"class_list":["post-30","post","type-post","status-publish","format-standard","hentry","category-as3","category-flash"],"_links":{"self":[{"href":"http:\/\/wahlers.com.br\/claus\/blog\/wp-json\/wp\/v2\/posts\/30","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/wahlers.com.br\/claus\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/wahlers.com.br\/claus\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/wahlers.com.br\/claus\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/wahlers.com.br\/claus\/blog\/wp-json\/wp\/v2\/comments?post=30"}],"version-history":[{"count":0,"href":"http:\/\/wahlers.com.br\/claus\/blog\/wp-json\/wp\/v2\/posts\/30\/revisions"}],"wp:attachment":[{"href":"http:\/\/wahlers.com.br\/claus\/blog\/wp-json\/wp\/v2\/media?parent=30"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/wahlers.com.br\/claus\/blog\/wp-json\/wp\/v2\/categories?post=30"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/wahlers.com.br\/claus\/blog\/wp-json\/wp\/v2\/tags?post=30"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}