tutorials flash library_flash_flex
Shared library
Following a previous tutorial on the subject, we have seen how to share a library (Classes, Symbols …) view post: Shared library in Flash 9 Flash 9.
We are going to see that we no longer need to call the ApplicationDomain getDefinition method to recuperate the definition of a class through the Class class
I will explain with some code:
// this code is found in the post on the shared library in the completeHandler function called once the swf is loaded. ... var domain:ApplicationDomain = loader.contentInfoLoader.applicationDomain; // I recuperate the definition of the Circle class contained in the swf file in a Class type object var CircleClass:Class = domain.getDefinition("Circle"); // allowing me therefore to instantiate a Circle type object var c:DisplayObject = new CircleClass(); addChild(c);
We know nowadays that each application is enclosed in what we call an application domain guaranteeing a certain level of security of the content. Except that we have seen that we can bring one application domain into another.
You have probably noticed in the Loader load method and in URLLoader (Flex 2) a second LoaderContext parameter. Thanks to the latter we will be able to bring the application domain of the loaded swf to that which loads it, here is an example:
function completeHandler(e:Event):void { // We check that the definition exists in the current and most unique application domain that is loaded output.text = "Circle Definition in the current domain : " + ApplicationDomain.currentDomain.hasDefinition("Circle"); // For those who wish to verify the loaded application domain // var domain:ApplicationDomain = e.target.applicationDomain; // ouput.appendText("Circle Definition in the loaded domain : " + domain.hasDefinition("Circle")); // This goes without comment var c:DisplayObject = new Circle(); c.x = 100; c.y = 250; var s:DisplayObject = new Square(); s.x = 10; s.y = 25; var calvin:BitmapData = new Calvin(); var bitmap:Bitmap = new Bitmap(calvin); bitmap.x = 250; bitmap.y = 100; addChild(bitmap); addChild(c); addChild(s); } var url:URLRequest = new URLRequest("library.swf"); var output:TextField = new TextField(); output.autoSize = TextFieldAutoSize.LEFT; output.text = "Loading..."; addChild(output); var loader:Loader = new Loader(); // Thanks to the LoaderContext class we open the loaded application domain to the loading application, other parameters of the LoaderContext relate to domain security var context:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler); // We pass this context to the load method and the whole movie is played loader.load(url,context);
You have probably noticed that we cannot really ‘type’ instances, this being a unique development environment (static).
This example is carried out in flash 9 preview which is considered by some as a software for do-it-yourselfers - I would rather say for those who like to experiment therefore I brought this example to a Flex 2 project, here is the example:
package { import flash.display.Loader; import flash.display.Sprite; import flash.events.*; import flash.net.URLRequest; import flash.system.ApplicationDomain; import flash.system.LoaderContext; import flash.display.DisplayObject; public class TestLoader extends Sprite { private var loader:Loader; public function TestLoader() { loader = new Loader(); var url:URLRequest = new URLRequest("library.swf"); var context:LoaderContext = new LoaderContext(false,ApplicationDomain.currentDomain); loader.contentLoaderInfo.addEventListener(Event.COMPLETE,completeHandler); loader.load(url,context); } private function completeHandler(event:Event):void { var c:DisplayObject = new Circle(); addChild(c); } } }
Be aware that for this code to work in Flex builder 2 you need to delete the strict mode otherwise it will tell you that the Circle class doesn’t exist, which is not the case in Flash 9 preview.
Here are the example source codes :
- Flash 9 : Library.zip
- Flex 2 : Library
By ITERATIF - BUGALOTTO Olivier (2006). You can find this tutorial and comments on the subject on my blog
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42


