Using SWC as RSL in Flex projects is a no brainer. Flash Builder and Flex ant task automate a lot of things for your convenience. Unfortunately, that’s not the case with pure AS3 projects. I read somewhere that you can use a special Frame tag and a factory class to load the RSL swf before you instantiate your document class. I tried that but I got mixed results; it didn’t always work in all cases especially when the SWC contains a lot of assets. I might be doing something wrong; I don’t know.
It turned out that you can use a “loader” SWF to load the RSL swf before your main SWFs to achieve the same thing. That left me with one problem, how to get the RSL swf out of the SWC. As we know, SWC is just another compression format, just like zip so any zip expander/extractor will work but manually extracting the SWC is going to be a pain in the @$$ and you’ll have to do it every time you update your SWC. This is where Ant becomes your best friend 😉
In this post, I’ll show you how to do it with Flash Builder & Ant.
Creating Projects
First, we create a new workspace and add build folder to store the published SWFs and then create the projects below:
MyLibs
– Flex library project. This is the RSL to be used byAppOne
andAppTwo
.AppOne
– AS3 projectAppTwo
– AS3 projectloader
– AS3 project that will load all other SWFs
Then we add a Car
class to MyLibs
project and compile the project so we have MyLibs.swc
in its bin/ folder.
App Projects
Now let’s open AppOne
project properties.
- Go to ActionScript Build Path settings
- Add
Mylibs.swc
from MyLibs project and set its linkage type “external” - Open
AppOne.as
- Instantiate
Car
class
package { import flash.display.Sprite; import mylibs.Car; public class AppOne extends Sprite { public function AppOne() { var car:Car = new Car(); trace(this,car); } } }
Repeat those steps for AppTwo
.
The Loader
Below is the loader.as
file that loads everything.
package { import flash.display.Loader; import flash.display.Sprite; import flash.events.Event; import flash.net.URLRequest; import flash.system.ApplicationDomain; import flash.system.LoaderContext; public class loader extends Sprite { private var _appsToLoad:Array = ["AppOne.swf","AppTwo.swf"]; public function loader() { addEventListener(Event.ADDED_TO_STAGE ,init); } private function init(event:Event):void{ loadLib(); } private function loadLib():void { var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadApps); loader.load(new URLRequest("MyLibs.swf"), new LoaderContext(false,ApplicationDomain.currentDomain)); } private function loadApps(event:Event):void { if(_appsToLoad.length > 0){ var loader:Loader = new Loader(); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadApps); loader.load(new URLRequest(_appsToLoad.shift()), new LoaderContext(false,ApplicationDomain.currentDomain)); } } } }
ANT Script
Here’s the ANT script that extracts MyLibs.swc
and put MyLibs.swf
in the build folder.
And here’s a longer version If you want to compile your projects with ANT as well.
Testing
When you open loader.swf
you should see the following trace output in flashlog.txt:
[object AppOne] [object Car] [object AppTwo] [object Car]
How do we know that Car
class didn’t get compiled into the app SWFs? Well, if you have a decompiler like Sothink SWFDecompiler, you can check it visually and see that Car
is not compiled into those SWFs.
Here’s the zip containing the samples [download id=”17″ format=”2″]
Also in this category ...
- » Website Baru
- » Global Dot di NodeJS
- » Compound Component
- » Membuat Public API di Komponen React dengan useImperativeHandle
- » useContext untuk Global State