Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-settings.php on line 472

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-settings.php on line 487

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-settings.php on line 494

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-settings.php on line 530

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-includes/cache.php on line 103

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-includes/query.php on line 21

Deprecated: Assigning the return value of new by reference is deprecated in /nfs/c03/h01/mnt/55667/domains/blog.michaeljbowen.com/html/wp-includes/theme.php on line 623
michaeljbowen.com » Modules in Flash
Home > Flash, Hacks > Modules in Flash

Modules in Flash

December 22nd, 2008

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)

Advanced ActionScript 3.0 Settings

Advanced ActionScript 3.0 Settings


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.

Flash, Hacks

  1. December 22nd, 2008 at 22:02 | #1

    What does the “j” stand for?

  2. December 22nd, 2008 at 22:10 | #2

    “jeepers, michaelbowen.com is already taken”

  3. December 22nd, 2008 at 22:56 | #3

    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.

  4. June 2nd, 2009 at 05:25 | #4

    Hi, good post. I have been wondering about this issue,so thanks for posting. I’ll definitely be coming back to your site.

  5. June 13th, 2009 at 19:13 | #5

    The best information i have found exactly here. Keep going Thank you

  6. June 15th, 2009 at 00:06 | #6

    Hi, gr8 post thanks for posting. Information is useful!

  1. No trackbacks yet.