One of the more powerful features of the flash engine is the ability to load flash programs within flash programs, through the use of the Loader class. Unfortunately communication with the loaded program is limited. While you can establish a LocalConnection object there is a limit to the traffic it can safely support.

A simple solution is to load the SWF file within your main program’s security domain. This has the benefit of exposing the public methods and properties of the loaded SWF to the loader and vice versa.

First extend the Loader class this class will be used to interact with the loaded file.

public class ParentChildLoader extends Loader

Next we must store the SWF file as a ByteArray. The variable path is a file path on the system. You could use a URLStream object instead of a FileStream for a http url.

var swfBytes:ByteArray = new ByteArray(); var file:File = new File(path); if (file.exists) { var loadStream:FileStream = new FileStream(); loadStream.open(file, FileMode.READ); loadStream.readBytes(swfBytes); loadStream.close(); }

Now that we have the SWF stored as a ByteArray, we load the SWF into our security domain and listen for the complete event.

var context:LoaderContext = new LoaderContext(); context.allowLoadBytesCodeExecution = true; addEventListener(Event.COMPLETE, onSwfLoad); loadBytes(swfBytes, context);

If you want to access the loaded SWF’s properties from the loader use the content property.

Object(this.content).foo(bar); Object(this.content).a = b;

If you want to access the loader’s public properties from the SWF use the parent property.

Object(this.parent).foo(bar); Object(this.parent).a = b;

This has many practical applications from allowing re-usability of common functions to taking some of the programming load off your creative team. A note of caution; the loaded SWF exists within your main program’s security domain so it is key that you only load files which you trust with this method.