Modules in Flash
Modules are SWF files that can be loaded by an application but cannot run indepedently. Modules are built in to Flex 3, but it takes a bit of finagling to use modules in Flash.
I have been able to use modules successfully in Flash. Below are the steps to follow. I have also included the sample source code:
http://michaeljbowen.com/examples/modulesInFlash.zip
Step 1
Grab the latest Flex SDK. I used 3.2:
http://www.adobe.com/products/flex/flexdownloads/
Step 2
In the Flash IDE (I’m using CS4), create a reference to the Flex SDK:
File > Publish Settings > Flash > Settings > Library Path
Add a reference to frameworks\libs (see the following image)
Step 3:
Create a module by creating a class that extends mx.modules.ModuleBase.
1 2 3 4 5 6 7 8 9 10 11 12 13 | import mx.modules.ModuleBase; ... public class ModuleAlpha extends ModuleBase implements IModuleAlpha { public function ModuleAlpha() { trace("ModuleAlpha created."); } public function addNumbers(a:Number, b:Number):Number { return a + b; } } |
Step 4:
Compile the module using the mxmlc command line compiler.
Note: If you’ve placed your source in subdirectories, you will need to let the compiler know your source path.
In my example, ModuleAlpha.as is located in the com.electrotank.modules package.
Thus, here is my command to compile:
1 | >mxmlc "-source-path+=../../../" ModuleBeta.as |
Step 5:
Create a “parent” application that will load in your module(s) via mx.modules.ModuleManager.
Here is an excerpt from the sample code provided:
1 2 3 4 5 6 | private const URL_MOD_ALPHA:String = "ModuleAlpha.swf"; ... _modules[URL_MOD_ALPHA] = ModuleManager.getModule(URL_MOD_ALPHA); IModuleInfo(_modules[URL_MOD_ALPHA]).addEventListener(ModuleEvent.READY, onModReady); IModuleInfo(_modules[URL_MOD_ALPHA]).addEventListener(ModuleEvent.ERROR, onModError); IModuleInfo(_modules[URL_MOD_ALPHA]).load(); |
Step 6:
Create an instance of your newly uploaded module using the factory of IModuleInfo:
1 2 | var modAlpha:IModuleAlpha = IModuleAlpha(IModuleInfo(_modules[URL_MOD_ALPHA]).factory.create()); trace(modAlpha.addNumbers(2, 3)); |
Done
That’s it!
Be sure to use your powers only for good and not evil.
What does the “j” stand for?
“jeepers, michaelbowen.com is already taken”
nice description. one additional note, in order to make use of some of the more power module features, such as reducing the size of the module SWF, you’ll need to use the flex compiler on the code that loads the module, as well as the module itself.
Hi, good post. I have been wondering about this issue,so thanks for posting. I’ll definitely be coming back to your site.
The best information i have found exactly here. Keep going Thank you
Hi, gr8 post thanks for posting. Information is useful!