Actionscript : Packaging graphics into swf
It took me some time but here is a complete tutorial to package your graphics into a single swf, in order to load them (fast) into a pure actionscript project.
And bonus: it does works for iOS too! (Haven’t test on app store yet, but it does work in a test package)
First of all, gather your pictures. I favor png for transparency, but it seems that there is several sub formats and I find 2 different problems on 2 different tools (png2swf and hxswfml, see my previous post for reference).
Hopefully, pngout (http://advsys.net/ken/utils.htm) is a wonderful tools to rewrite png from png, either to optimize size, but also to rewrite them in a format usable by tools that does not cover all the sub formats.
For hxswfml, which will allow us to compile our assets, I use pngout with the /force /c6 flags. For a whole directory, my batch file is:
FOR /F "tokens=*" %%G IN ('dir *.png /s /b') DO (pngout "%%G" /force /c6)
(Under widows ... i guess linux users will now how to do this)
Compiling into a single swf
Then, you need to create a xml file that contains all yours assets:
- <sound file="C:/MyDir/MyMusic.mp3" class="MyMusic.mp3"/>
- <bitmap file="C:/MyDir/MyPicture.png" class="MyPicture.png"/>
Class name does not need to equal the file name, but I find it easier.
Then, run hxswfml (https://code.google.com/p/hxswfml/) with this command line:
hxswfml.exe xml2lib MyXMLFile.xml assets.swf
You obtain an assets.swf file that contains both your picture and your music!
Loading picture in actionscript
First you need to load the assets.swf file into your applicationDomain
- var theLoader:Loader = new Loader;
- theLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, MySwfIsLoadedFunction);
- theLoader.load(new URLRequest(MySwfName), new LoaderContext(false, ApplicationDomain.currentDomain, null));
Just wait the call of MySwfIsLoadedFunction before using graphics. They are loaded as class in currentDomain, meaning you can instantiate them by their names:
- var graphClass:Class = getDefinitionByName("MyPicture.png") as Class;
- var MyPicture:DisplayObject = new graphClass() as DisplayObject;
You can then add them to your stage!
You can get your music the same way:
- var soundClass:Class = getDefinitionByName(i_name) as Class;
- var soundObject:Sound = new soundClass() as Sound;