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
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.

  Post a Comment
Name :
Email :
Comment :
 
Comments: 10
  Kiran Babu
Very useful
 
  Hadi
I love you :) you just simply explained the swc and all I needed to know about it. before I know this, I used to compile my classes in an external SWF file and then I used a lot of complex byteArray codes to initialize the swf file and use the class in it but it was all pain and pain and confusion for me, my team and customers! now, everything will be super better. thanks to you for sharing this.
 
  fetusbata
Yes! thanks a lot friend, what i'm searching about swc is now what i found from this site. Thank you so much!
 
  Ali
Hi, I need SWC to protect my code, I did the things you explained but when imported it I just got the graphic that I created in the movieclip that then I have exported into SWC!! and couldn't use the classes that I imported in the Packager.as

You know What I want to do? I have created a class that accepts 20 inputs, now I want convert it into SWC to protect it and set its inputs via the SWC file that includes my class...

What should I do? please explain a little more for me so that I can do it via Flash CS4

Really thanks for your great posts :)
 
  Lu
Hi Ali,
Make sure you reference the classes you want in Packager.as. Just importing them is not enough.
For example:
make a private variable typed to the class
OR
put all classes in a variable array (var a:Array = [Class1,Class2,Class3];)
 
  saki
Hello LU

thanks alot for great post ,

i am facing one small issue r with this method , i am trying to create a API for some third party , i have one method which return vector , my API uses vector as return type , when i try to create component defination , it throws this error ....1046: type was not found or was not a compile time constant : Vector , i am using CS4 so it has vector capability in build .

plzz help.


 
  Lu
Hi Saki,

I did a quick test with Vector and it seem to work. Make sure:

- Flash publish setting must be set to Flash 10.
- In component definition menu, maybe try set 'Require minimum player
version' to player 10.

Hope this helps,
Lu
 
  Joe
Hi Lu! :)

I've created a SWC using compc in an Ant task in FDT, but this file won't show up in the Components :( I've tried fooling it by adding a MovieClip to it but that doesn't work. Someone said I might need to create a manifest file... Do you know anything about this?

Thanks :) Joe
 
  Joe
Hi again Lu,

ignore my last comment I was being fick... Basically was trying to work out a way of importing a Pixel Bender binary into CS3, but realised that since CS3 doesn't support Flash 10 anyway that's completely pointless ;-)

Hope you're well!

:-Joe
 
  Lu
Hehe, i was just going to write to you to use CS4 :P
I donno how you can import a swc that doesn't have component icon into CS3.