// you’re reading...

ActionScript 3.0

Flash/Flex Integration: SWFLibrary v1.5

Usually I like to post glamorous stuff, like 3D swirly things and experiments. Occasionally, however, I feel the need to release a little code in the interest of helping a few people out. This code is a review of sorts, since I already posted a similar utility back in June. However, since then I have been expanding my code library and I thought I’d share the latest and much improved version. It’s really a simple concept, but having this in my toolbox has lightened my workload as an ActionScripter considerably.

Advantages

This utility allows you to publish any symbol from Flash and access it at runtime from an ActionScript-only project, such as in Flex Builder. This has a few advantages:

  • Easy storage of many assets in one file
  • Ability to change graphics without recompiling the main application
  • Faster load times with only one HTTP request (as opposed to many separate images and sounds)
  • Ability to import vector graphics and animations at runtime
  • Smaller main SWF size

The one disadvantage that I can think of is that you’ll need to recompile your library SWF every time you make a change. However, I think the advantages above outweigh this.

Differences in v1.5

Here are the main differences from the last version:

  • The class is no longer static. This allows for more than one instance of SWFLibrary at a time.
  • The class can now return instances of Sprites, MovieClips, and Sounds from a library SWF.

Usage

1 - In Flash, select “Export for ActionScript” in the symbol’s Linkage settings in the Library (ex. ‘Character’).
2 - Publish the Flash file as a SWF.
3 - Load the SWF into an instance of SWFLibrary
4 - Access the symbol through SWFLibrary, using the class name chosen in Step 1.

A SWFLibrary can be used with four lines of code. Here’s what you’ll need:

// creates an instance of SWFLibrary, adds a listener, and loads a SWF
var gameAssets:SWFLibrary = new SWFLibrary
gameAssets.addEventListener( Event.COMPLETE, handleAssetsLoaded );
gameAssets.load( "myCustomGameAssets.swf" );
 
// accesses the asset (assumes there is a symbol exported as 'Character' in the loaded SWF, as seen in step 1 above)
// this code should run after the Event above has fired
var mySprite:Sprite = gameAssets.getSprite( "Character" );
addChild( mySprite );
 
// gets the asset as a MovieClip (assuming, of course, that it actually is a movieclip)
var myMC:MovieClip = gameAssets.getSprite( "Character" );
addChild( myMC );
 
// getting a sound
var mySound:Sound = gameAssets.getSound( "SoundLinkageNameHere" );
mySound.play();
// === ===

After a symbol has been retrieved from SWFLibrary, it may be treated like any other object.

Download

DOWNLOAD SOURCE

So have fun with this. Hopefully it saves you as much time as it saves me.

Discussion

6 comments for “Flash/Flex Integration: SWFLibrary v1.5”

  1. That’s really quite helpful and solid. Thanks, Zack!

    Posted by Benjamin | August 21, 2008, 1:34 pm
  2. Thanks to share this source! It saves me!

    Posted by LutinCapuche | August 21, 2008, 5:07 pm
  3. Nice! I extended it with EmbedSWFLibrary. Especially useful for single-file haXe apps.

    Posted by Gust | August 22, 2008, 6:57 am
  4. […] 来自 […]

    Posted by iTamt.cn » 博客文章 » SWFLibrary v1.5 | August 22, 2008, 10:31 pm
  5. How come your site is redirected from ErinGipson.com?

    Posted by j | September 11, 2008, 10:37 am
  6. I used to host her site until she took it down. I guess her domain still points to my server.

    Kinda interesting though. I’ll have to look into it.

    Posted by Zack Jordan | September 11, 2008, 10:46 am

Post a comment

Twitterpated