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 byAppOneandAppTwo.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.swcfrom MyLibs project and set its linkage type “external” - Open
AppOne.as - Instantiate
Carclass
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
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.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<?xml version=“1.0″ encoding=“utf-8″?> <project name=“build” basedir=”/Users/boss/workspaces/fl/personal/blogposts/as3rsl”> <macrodef name=“create-rsl”> <attribute name=“rsl-dir” /> <attribute name=“swc-dir” /> <attribute name=“swc-name” /> <sequential> <unzip src=”@{swc-dir}/@{swc-name}.swc” dest=”@{rsl-dir}”> <patternset> <include name=“library.swf” /> </patternset> </unzip> <move file=”@{rsl-dir}/library.swf” tofile=”@{rsl-dir}/@{swc-name}.swf” /> </sequential> </macrodef> <!– extract swc –> <create-rsl rsl-dir=”${OUTPUT_DIR}” swc-dir=”${basedir}/MyLibs/bin” swc-name=“MyLibs” /> </project> |
And here’s a longer version If you want to compile your projects with ANT as well.
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 |
<?xml version=“1.0″ encoding=“utf-8″?> <project name=“build” default=“all” basedir=”/Users/boss/workspaces/fl/personal/blogposts/as3rsl”> <property name=“FLEX_HOME” value=”/Applications/Adobe Flash Builder 4.6/sdks/4.6.0″ /> <property name=“SDK_VERSION” value=“4.6.0.23201″ /> <taskdef name=“mxmlc” classname=“flex.ant.MxmlcTask” classpath=”${FLEX_HOME}/ant/lib/flexTasks.jar” /> <taskdef name=“compc” classname=“flex.ant.CompcTask” classpath=”${FLEX_HOME}/ant/lib/flexTasks.jar” /> <property name=“OUTPUT_DIR” value=”${basedir}/build” /> <property name=“RSL_SWC” value=”${basedir}/MyLibs/bin/MyLibs.swc” /> <property name=“PLAYERGLOBAL_SWC” value=”${FLEX_HOME}/frameworks/libs/player/11.1/playerglobal.swc” /> <target name=“all”> <antcall target=“MyLibs” /> <antcall target=“AppOne” /> <antcall target=“AppTwo” /> <antcall target=“loader” /> </target> <!– compile & extract MyLibs –> <macrodef name=“create-rsl”> <attribute name=“rsl-dir” /> <attribute name=“swc-dir” /> <attribute name=“swc-name” /> <sequential> <unzip src=”@{swc-dir}/@{swc-name}.swc” dest=”@{rsl-dir}”> <patternset> <include name=“library.swf” /> </patternset> </unzip> <move file=”@{rsl-dir}/library.swf” tofile=”@{rsl-dir}/@{swc-name}.swf” /> </sequential> </macrodef> <target name=“MyLibs”> <compc output=”${basedir}/MyLibs/bin/MyLibs.swc” include-classes=“mylibs.Car”> <source-path path-element=”${basedir}/MyLibs/src” /> <external-library-path file=”${PLAYERGLOBAL_SWC}” append=“true” /> </compc> <!– extract swc –> <create-rsl rsl-dir=”${OUTPUT_DIR}” swc-dir=”${basedir}/MyLibs/bin” swc-name=“MyLibs” /> </target> <!– compile AppOne –> <target name=“AppOne”> <mxmlc file=”${basedir}/AppOne/src/AppOne.as” output=”${OUTPUT_DIR}/AppOne.swf” static-rsls=“false” debug=“true” accessible=“true”> <load-config filename=”${FLEX_HOME}/frameworks/flex-config.xml” /> <source-path path-element=”${basedir}/AppOne/src” /> <external-library-path file=”${PLAYERGLOBAL_SWC}” append=“true” /> <external-library-path file=”${RSL_SWC}” append=“true” /> </mxmlc> </target> <!– compile AppTwo –> <target name=“AppTwo”> <mxmlc file=”${basedir}/AppTwo/src/AppTwo.as” output=”${OUTPUT_DIR}/AppTwo.swf” static-rsls=“false” debug=“true” accessible=“true”> <load-config filename=”${FLEX_HOME}/frameworks/flex-config.xml” /> <source-path path-element=”${basedir}/AppTwo/src” /> <external-library-path file=”${PLAYERGLOBAL_SWC}” append=“true” /> <external-library-path file=”${RSL_SWC}” append=“true” /> </mxmlc> </target> <!– compile loader –> <target name=“loader”> <mxmlc file=”${basedir}/loader/src/loader.as” output=”${OUTPUT_DIR}/loader.swf” static-rsls=“true” accessible=“true”> <load-config filename=”${FLEX_HOME}/frameworks/flex-config.xml” /> <source-path path-element=”${basedir}/loader/src” /> <external-library-path file=”${PLAYERGLOBAL_SWC}” append=“true” /> </mxmlc> </target> </project> |
Testing
When you open loader.swf you should see the following trace output in flashlog.txt:
|
1 2 3 4 |
[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 → as3rsl.zip
Also in this category …
- » Apa yang Dibutuhkan untuk Membuat Social Game
- » Belajar Menjadi Programmer
- » Make On-device Debugging Easier with iFunBox
- » Easy Inheritance & Modularity in Lua
- » How to Enable ANT view on FlashBuilder 4.5
