原文见:http://blog.csdn.net/genius840215/archive /2010/03/25/5411732.aspx
一般我们生成的swf有3种,一种是Flex mxml主应用编译得到,第2种类是用Flex SDK编译as类得到,还有一个就是fla在Flash CS3内运行生成。
因为第3种的调用方法同第2种,这里就说明前2种swf在Flex项目内的应用。
HelloWorld1.mxm
- <?xml version="1.0" encoding="utf-8"?>
- <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
- layout="absolute" >
- <mx:Script>
- <!--[CDATA[
- public function sayHello():void{
- trace("HelloWorld from mxml");
- }
- ]]-->
- </mx:Script>
- </mx:Application>
HelloWorld2.as
- package {
- public class HellowWorld2{
- public function sayHello(): void {
- trace("HelloWorld from AS" );
- }
- }
- }
- <? xml version = "1.0" encoding = "utf-8" ?>
- < mx:Application xmlns:mx = "http://www.adobe.com/2006/mxml" layout = "absolute" >
- < mx:Script >
- <!--[CDATA[
- public function CompleteHandler1(evt:Event):void{
- Object(evt.target.content.application).sayHello(); //区别
- }
- public function CompleteHandler1(evt:Event):void{
- Object(evt.target.content).sayHello(); //区别
- }
- ]]-->
- </ mx:Script >
- < SWFLoader id = "hw1" source = "HelloWorld1.swf" complete = "CompleteHandler1(event)" />
- < SWFLoader id = "hw2" source = "HelloWorld2.swf" complete = "CompleteHandler2(event)" />
- </ mx:Application >
HelloWorld1.mxml和HelloWorld2.as分别用Flex SDK编译成swf,然后在app.mxml内调试。
可以看出第一种情况生成的swf因为是通过mxml生成的,e.target.content其实是一个SystemManager类对象,必须要 通过它的application才能访问swf内部方法,而用as类直接生成的swf通过e.target.content就能直接访问。