views:

123

answers:

2

Hi,

Lets say I have two swfs A and B, and at runtime, swf A loads swf B, and I wish to share code between them, to minimize file size and download times.

If swf B has some code (say. com.blah.HelloWorld), I tell the compiler to have swf B's source in swf A's classpath, but only do a compile-time link and not actually compile com.blah.HelloWorld into swf A.

This works, and I have tried it, using a the -includes and -externs compiler options.

However, My problem is that I wish to do this the other way. i.e. swf A and B (and potentially swf C) all need com.blah.HelloWorld, but I want com.blah.HelloWorld to be compiled into just swf A, have it as an external reference in swf B ( and potentially C as well.)

I tried doing this using the externs and includes, but I get ReferenceErrors when I do this.

I want to do this without a having a separate rsl, so I can reduce the number of http requests. Is this possible?

A: 

I'm not sure I'm quite understanding your question, but I think you can use the AS3 Loader class to do what you want. The format would be something like this - let's say we're creating your main app (which will be called "a.swf") and you want to access methods and properties that have been compiled into another app (called "b.swf"), you'd do this:

var SWFB:Object; // empty at first as a placeholder.

var url:URLRequest  = new URLRequest("b.swf");
var l:Loader = new Loader();
l.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
l.load(url);

function loaded(e:Event):void {
 SWFB = e.currentTarget.content as Object;
 initApp();
}

function initApp():void {
 SWFB.someMethodCall();
}

... and I THINK that'll work. I can't test it right now, but try it and let me know. Basically you're going to be loading b.swf in as a basic object, and you can then call methods against that object.

Please let me know if that worked, and if not I can refine it for you tomorrow.

Myk
+1  A: 

You can split your flex application into modules.

Or you can access individual classes from an SWF loaded at runtime using the getDefinition method of the ApplicationDomain class:

var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
loader.load(new URLRequest("c.swf"));
//..
private function onLoad(e:Event):void
{
  var domain:ApplicationDomain = LoaderInfo(e.target).applicationDomain;
  var Type:Class = domain.getDefinition("pack.MyComponent") as Class;
  var myBox:Sprite = new Type();
  addChild(myBox);
}
Amarghosh