![]()
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.
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:
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.
Here are the main differences from the last version:
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.
So have fun with this. Hopefully it saves you as much time as it saves me.
That’s really quite helpful and solid. Thanks, Zack!
Thanks to share this source! It saves me!
Nice! I extended it with EmbedSWFLibrary. Especially useful for single-file haXe apps.
[…] 来自 […]
How come your site is redirected from ErinGipson.com?
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.