Flex从swf中加载资源和类
package{import flash.display.Sprite;public class TestAssets extends Sprite{public function TestAssets(){}[Embed(source="/assets/images/volume_back.png")]public static var test1:Class;}}
?
在Flash中新建项目:test.fla,导入一个图片到库中,拖到舞台,然后F8,创建影片剪辑,将该影片剪辑的AS链接起名为testClass。
因为还想要打包进去一个XML,于是创建一个空元件,将其AS链接起名为ClassA,并创建ClassA.as:
package {import flash.display.MovieClip;public class ClassA extends MovieClip {public function ClassA() {// constructor code}public static var theXML:XML = <root><item title="title123"><name>Jason</name></item></root>;public var theVar:String = "variable" + new Date().time;}}
?
?
Flex代码:
<?xml version="1.0" encoding="utf-8"?><s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)"><fx:Script><![CDATA[import mx.events.FlexEvent;import spark.components.Image;protected function application1_creationCompleteHandler(event:FlexEvent):void{load1();load2();}// load assets from Flex AS project swf. test1 is a static class for image Embed.private function load1():void{var loader:Loader = new Loader();loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void{trace("completed");// TestAssets 为Flex AS项目主文件的名称。var asset:Object = loader.contentLoaderInfo.applicationDomain.getDefinition("TestAssets");var img:Image = new Image();img.source = asset.test1;group.addElement(img);});loader.load(new URLRequest("TestAssets.swf"));}// load assets from Flash project swf. private function load2():void{var loader:Loader = new Loader();loader.contentLoaderInfo.addEventListener(Event.COMPLETE, function(e:Event):void{trace("completed");// "testClass" 为某个图片元件的AS链接。var asset:Object = loader.contentLoaderInfo.applicationDomain.getDefinition("testClass");var img:Image = new Image();img.source = new asset();group.addElement(img);// ClassA 为空元件的AS链接。var cla:Object = loader.contentLoaderInfo.applicationDomain.getDefinition("ClassA");//var cla1:Object = loader.contentLoaderInfo.applicationDomain.getDefinition("test");trace(cla.theXML);var claInstance:Object = new cla();trace(claInstance.theVar);});loader.load(new URLRequest("test.swf"));}]]></fx:Script><fx:Declarations><!-- 将非可视元素(例如服务、值对象)放在此处 --></fx:Declarations><s:VGroup id="group"></s:VGroup></s:Application>
?
debug模式运行:
<root>
? <item title="title123">
? ? <name>Jason</name>
? </item>
</root>
variable1386586432855