Importing a swc into Flex Builder for pure actionscript projects
I have previously written a post about the potential bug which seems to occur when importing multiple swcs into Flex Builder 3 for pure actionscript projects. Despite this I cannot express how handy it is to be able to import swcs into Flex Builder; for one reason because it renders the Flash IDE even more redundant (which is a good thing).
Whilst talking to a friend who has recently started using Flex Builder for AS projects I discovered that he hadn’t as yet used the method of importing swcs to gain access to graphics, sounds, animations, etc which have been created in Flash IDE.
So far he has been using the Loader class to import the swf at runtime and then to gain access to the individual items in the fla/swf library. There are obvious advantages to this method, for example loading the swf at runtime avoids the increased file size of your project which you will incur from importing the swc which will add the graphics/sounds at compile time.
However for smaller projects where file size isn’t an issue or for importing the fundamental graphical elements which will be re-used throughout the project a swc is a quick and easy solution.
Firstly when creating you or a designer is creating the graphical elements in the flash library you can also add a class path to each movieclip or asset in the library.
1) Right click on the assets in the library and click Linkage…..
2) Tick the checkbox marked Export for Actionscript
3) Enter a class path which relates to the path structure of your application into the Class text field e.g. uk.co.ultravioletdesign.graphics.ThisAsset - nb If you just put a simply identifier/class name without a full class/package path (e.g. ThisAsset) the item will be available throughout your application which sounds useful but is the same as making classes public which if avoidable is good practice and good for application performance.
This also enables you to organise your assets in the Flash library into convenient packages. For example putting all sound assets in uk.co.ultravioletdesign.assets.sounds and all graphics into uk.co.ultravioletdesign.assets.graphics. You don’t need to worry about creating the equivalent folders and packages within Flex Builder because they will be discreetly generated when the swc is imported.
4) Go to Publish Settings (File > Publish Settings) and then click the Flash tab. Providing the Fla is AS3 the checkbox Export SWC will be available. Check it!
5) Now once the movie is published a .swc file will be snuggling up next to your .swf which you can promptly delete (if you feel the need).
In Flex Builder:
6) Right click on your Actionscript project in the Flex Navigator window and click properties. Click on Actionscript Build Path and then on the Library path tab.
7) Click on the Add SWC button and browse to your newly generated .swc file.
Flex now adds your library assets to your project and these are made available within classes using the import directive:
import uk.co.ultravioletdesign.graphics.ThisAsset;
var thisAsset : ThisAsset = new ThisAsset();
Using this method in my experience has been invaluable and a massive timesaver. Despite this it is very useful to have the knowledge to be able to import assets from swfs at runtime. There is very handy for other strategies and techniques such as the importing fonts at runtime. This article by Chris Rebstock has been very valuable.
G

11. October 2008 at 14:50
[...] swc. It’s a really nice little method and if you wanna learn about it Ultraviolet Design have a great post about it here. I’m pointing you towards their post as this post is not going to be about that. Instead [...]
11. October 2008 at 14:53
hey gareth
thanks for this post, very useful. i had a few problems getting my swc to export properly but i found out why in the end and took the time to post a solution here as a kind of addendum to your post:
http://richtextformat.co.uk/blog/?p=241
6. November 2008 at 22:05
This was such a tremendous help - thanks so much!