| Lu Aye's Blog, images and projects: Feed |
LuAye.com |
| RSS Feed | |
| | All | Life | Ideas and thoughts | Reviews | Shout out | News | Coding | |
| <Previous Post | Next Post> |
| How to make a SWC in Flash Coding [Wed, 12 Aug 2009 22:26:12 +0200] |
| CS3 and CS4 |
| What is a SWC
a SWC is a compiled flash file similar to SWF, but it also include implement/interface definitions, called catalog. You can import the SWC in flash/flex and use its library assets and classes while keeping strong typing. You can not compile a swf file in another flash file, you would have to load it on run time, etc. SWCs are popularly used in Flex but I personally find it useful for flash these days. Why make a SWC Like SWF, it doesn't expose your inside code but still allows others to use your code's API/interface. Faster compile time - because code in your SWC is already compiled, when you compile your final flash it is faster. It also works around another problem... In a big flash project with lots of classes, flash simply refuse to compile when it hit its mysterious limit. It doesn't even give me any errors, just finishes compiling and the final SWF is about 50kb, which normally should be about 600kb. The only work around I found is to either split the project into different parts SWFs or precompile some of the code as SWCs. How to export to SWC from Flash CS3/CS4 (finally!) - Create a 'Packager' class which import all the class you want to export to. You need to actually reference by either putting all in an array or announcing a variable, just referring a class with 'import' seems to be not enough. - Create an empty flash file. (in same location as Packager.as to simplify) - Create an empty MovieClip and name it to whatever you want to call the SWC. - Draw a simple graphic inside - to describe the component (this is only needed if you are going to import it back into flash) recommended to be a simple square box with text saying the name. - Right click on MovieClip and choose 'properties' then choose 'Export for ActionScript' with Class set to the Packager class you have made on first step. - Right click on the MovieClip in library and choose 'Component Definition' - in 'Class' field, add the Packager class that you have made (again). - Choose icon you want to show by clicking on icon - this doesnt seem to serve anything else. - Tick 'display in components panel' to make sure it shows up in flash components panel... - Click OK - at this point it will already try to compile your Packager class and throw if any errors. - You should end up with a movieClip in library that has the same icon you chose in the previous dialog. - Optionally, If you want to add assets from library into the swc, the only way I found is to add all of them in the SWC movieclip you are making. Add all after first frame. - Right click on the movieClip and choose 'Export SWC' - AND... you got the SWC Here is a SAMPLE swc making fla with Packager.as in it... Sample file here How to import SWC to Flash CS4 In CS4 only, you can go to publish settings > Flash > Settings > 'Library Path' tab > click on the red swc logo to point to your SWC. You can now use the classes and assets from the SWC If you want to have your swc permanently show up in components panel copy the SWC file to: C:\Program Files\Adobe\Adobe Flash CS4\Common\Configuration\Components\ (where flash cs4 location may be different on your system) How to import SWC to Flash CS3 In CS3, there is no way to import your SWC from interface, however... - Copy the swc into C:\Program Files\Adobe\Adobe Flash CS3\en\Configuration\Components\ (where flash cs3 location may be different on your system) - Restart flash. - You should now see your component in the components panel (Window>Components) - Drag it into your library to start using the classes/assets (you should be able to see the graphic you made in library preview as well) IMPORTANT UPDATE: When importing, do not put the SWC in the same folder as your FLA file OR in any of the class package folders. This somehow confuses flash to use the SWC file but does not actually import properly. You will get a 'ReferenceError' at run time (it will compile). Hope you'll be able to make your own SWCs. The main reason I had to look for ways to make SWCs is because I was unable to compile my current project at work (as mentioned above) as a reward, it not only compiles now but does it faster. - it actually used to take 50seconds to compile, now about 35secs... my workstation = crap. |