动态加载swf并进行交互

综合http://blog.csdn.net/liyong199012/article/details/6085545

http://blog.sina.com.cn/s/blog_7e2d7da8010130gt.html

http://blog.csdn.net/hu36978/article/details/4718840

下面转自     http://bbs.9ria.com/thread-133293-1-1.html


总结下as3反射的相关用法,主要是四个函数:
flash.system.ApplicationDomain.getDefinition(linkName:String):Class;     //flashcs元件库中的某个元件链接名称
flash.utils.getDefinitionByName(name:String):Object;    //返回name参数指定的类的类对象引用
flash.utils.getQualifiedClassName(value:*):String;    //返回对象的完全限定类名
flash.utils.getQualifiedSuperClassNameTest;    // 返回value参数指定的对象的基类的完全限定类名。

在as3.0中,你会发现在flash.utils包中有一系列函数提供了反射的功能。主要包含以下功能:
* 确定对象的类
* 确定接口声明的常数和方法
* 获取类的成员、构造函数、方法、父类信息
* 在运行时根据类名创建类的实例
* 在运行时根据成员名称来调用对象成员
* 在运行时根据方法名称来调用对象方法

看代码:
Main.as类

  1. package
  2. {
  3. import flash.display.MovieClip;
  4. import flash.display.Shape;
  5. import flash.display.Sprite;
  6. import flash.system.ApplicationDomain;
  7. import flash.utils.getDefinitionByName;
  8. import flash.utils.getQualifiedClassName;
  9. import flash.utils.getQualifiedSuperclassName;

  10. import test.IPerson;
  11. import test.PersonImplA;

  12. /**
  13. * as3反射实例
  14. * @author xuechong
  15. * @language as3 fp9
  16. * @date 2012.06.03
  17. * @QQ群交流 241294271
  18. * */
  19. public class Main extends Sprite
  20. {
  21. //private var _personAersonImplA;

  22. public function Main()
  23. {
  24. //getDefinitionFunc();
  25. getDefinitionByNameFunc();
  26. //getQualifiedClassNameFunc();
  27. //getQualifiedSuperClassNameFunc();
  28. }

  29. /**
  30. * flash.system.ApplicationDomain类中的一个函数
  31. * aa字符串就是flashcs元件库中的链接名称(也就是元件绑定的类的名字)
  32. */
  33. public function getDefinitionFunc():void
  34. {
  35. var app:ApplicationDomain = ApplicationDomain.currentDomain;
  36. try
  37. {
  38. var cls:Class = app.getDefinition("aa") as Class;
  39. var mc:MovieClip = new cls() as MovieClip;
  40. mc.y = 300;
  41. this.addChild(mc);
  42. }
  43. catch(error:ReferenceError)
  44. {
  45. trace("反射错误", error.message);
  46. }
  47. }

  48. /**
  49. * flash.utils.getDefinitionByName(name:String):Object
  50. * 返回name参数指定的类的类对象引用
  51. * 注:如果是自定义类则要先定义上此类的引用
  52. * */
  53. public function getDefinitionByNameFunc():void
  54. {
  55. var ClassReference:Class = getDefinitionByName("test.PersonImplA") as Class; //[class PersonImplA]
  56. var obj:IPerson = new ClassReference() as IPerson; //[object PersonImplA]
  57. obj.sayHello();
  58. }

  59. /**
  60. * flash.utils.getQualifiedClassName(value:*):String
  61. * 返回对象的完全限定类名
  62. * */
  63. private function getQualifiedClassNameFunc():void
  64. {
  65. var sh:Shape = new Shape();
  66. trace(getQualifiedClassName(sh)); //flash.display::Shape

  67. var aersonImplA = new PersonImplA();
  68. trace(getQualifiedClassName(a)); //test:ersonImplA

  69. trace(getQualifiedClassName(this)); //Main
  70. }

  71. /**
  72. * flash.utils.getQualifiedSuperClassNameTest
  73. * 返回value参数指定的对象的基类的完全限定类名。
  74. * 即返回继承关系上的父类
  75. * */
  76. private function getQualifiedSuperClassNameFunc():void
  77. {
  78. var sh:Shape = new Shape();
  79. trace(getQualifiedSuperclassName(sh)); //flash.display:isplayObject

  80. var aersonImplA = new PersonImplA();
  81. trace(getQualifiedSuperclassName(a)); //flash.display::Sprite

  82. trace(getQualifiedSuperclassName(this)); //flash.display::Sprite
  83. }

  84. }
  85. }
复制代码


IPerson.as接口
  1. package test
  2. {
  3. public interface IPerson
  4. {
  5. function sayHello():void; 
  6. }
  7. }
复制代码


PersonImplA.as类

  1. package test
  2. {
  3. import flash.display.Sprite;

  4. public class PersonImplA extends Sprite implements IPerson
  5. {
  6. public function PersonImplA()
  7. {

  8. }

  9. public function sayHello():void

  10. trace("This is PersonImplA"); 
  11. }

  12. }
  13. }
复制代码

PersonImplB.as类
  1. package test
  2. {
  3. import flash.display.Sprite;

  4. public class PersonImplB extends Sprite implements IPerson
  5. {
  6. public function PersonImplB()
  7. {

  8. }

  9. public function sayHello():void

  10. trace("This is PersonImplB"); 
  11. }


  12. }
复制代码


下面是总结上面几个网址去动态加载swf进行交互的demo


package {
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.net.URLRequest;
import flash.system.ApplicationDomain;
import flash.system.LoaderContext;


public class LoadSwfExample extends Sprite {
private var loader:Loader;
private var completeLoaded:Boolean;
private var urlRequest:URLRequest;
private var getSwf:MovieClip;//对swf对象的引用变量
public function LoadSwfExample() {
loader= new Loader();
urlRequest=new URLRequest("Loaded.swf");// 该文档与Loaded.swf在一个目录

/**
* 第二种方式才需要加上
* **/
var context : LoaderContext = new LoaderContext(true, ApplicationDomain.currentDomain);
/* 加载到同域(共享库) */
context.applicationDomain = ApplicationDomain.currentDomain;
//context.applicationDomain = new ApplicationDomain(ApplicationDomain.currentDomain);

//下面我只注册个complete事件,对IOErrorEvent的事件在实际操作时也要注册,以便检测错误
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
loader.load(urlRequest);
stage.addEventListener(MouseEvent.MOUSE_DOWN,onDown);//主要是改变加载进来的swf的背景颜色
}
private function onComplete(e:Event=null):void {

// 第一种方式下面这句很关键, 将加载进来的Loaded.swf强制转化为MovieClip类型
getSwf=MovieClip(e.currentTarget.content);

//第二种方式
var runtimeClassRef:Class = getClass("Loaded");
getSwf = new runtimeClassRef() as MovieClip;

//把主swf传给动态加载的swf
getSwf.getParent(this);
//然后可以将其当做影片剪辑来使用 也可以操作swf里的全局变量
loader.contentLoaderInfo.removeEventListener(Event.COMPLETE,onComplete);
getSwf.x=100;
getSwf.y=200;
addChild(getSwf);
completeLoaded=true;

}
private function onDown(e:MouseEvent):void {

if (completeLoaded) {
getSwf._text+=1;//这里操作了_text全局变量
//调用Loaded.swf里的drawbackground()方法
getSwf.drawBackground(Math.random()*0xFFFFFF);//随机颜色值

} else {
trace("正在加载,请等待...");
}
}

/**
*该方法得是public,不然动态加载的swf调用不了该方法 
**/
public function moveSWF():void{
getSwf.x+=10;
getSwf.y+=10;
}

//使用反射的方式
private function getClass(className:String):Class
{
//return loader.contentLoaderInfo.applicationDomain.getDefinition(className)  as  Class;
try
{
return loader.contentLoaderInfo.applicationDomain.getDefinition(className)  as  Class;
} catch (p_e : ReferenceError)
{
trace("定义 " + className + " 不存在");
return null;
}
return null;
}

}
}


Loaded.swf的代码如下


package {

//该文档的大小是400*200的
// 为了使该swf被夹在后,其他swf能够调用相关变量 则,则该变量必须设为public的公共属性
//强制转换失败的原因
import flash.display.MovieClip;
import flash.display.Shape;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFormat;


public class Loaded extends MovieClip {
public var _text:Number;//文本内容, 其在加载后的swf里要用的 故将其设为public 
private var shape:Shape;
public var _textField:TextField;
private var __parent:Object;



public function Loaded() {
_text=0;
shape=new Shape()  ;//shape放在外面
drawTextField();
drawBackground(0x6699FF);
addChild(shape);//end draw 
addChild(_textField);

}

private function drawTextField():void {
_textField=new TextField  ;
_textField.border=true;
_textField.background=true;
_textField.borderColor=0x000000;
_textField.backgroundColor=0xFFFFFF;
_textField.width=300;
_textField.height=100;
_textField.defaultTextFormat=new TextFormat("Arial",16,0x000000);
_textField.restrict="0-9";
_textField.text=String(_text);
_textField.x=50;
_textField.y=50;
_textField.addEventListener(MouseEvent.CLICK,function():void{
trace(__parent);
if(__parent){
__parent.moveSWF();
}
});
}
//创建背景矩形 在其他swf可以改变背景颜色, 故将其设为public 
public function drawBackground(color:uint):void {
_textField.text=String(_text);
shape.graphics.clear();
shape.graphics.lineStyle(2,0);//定义矩形边宽
//画矩形
shape.graphics.beginFill(color);
shape.graphics.drawRect(2,2,398,198);
shape.graphics.endFill();
}

public function getParent(_parent:Object) : void {
__parent=_parent;//通过__parent 就可以使用文档类里的方法和属性了。
}

public function setTextFun(msg:String):void{
trace("msg =" + msg);
}

}
}




SWFTools is a collection of utilities for working with Adobe Flash files (SWF files). The tool collection includes programs for reading SWF files, combining them, and creating them from other content (like images, sound files, videos or sourcecode). SWFTools is released under the GPL. The current collection is comprised of the programs detailed below: • PDF2SWF A PDF to SWF Converter. Generates one frame per page. Enables you to have fully formatted text, including tables, formulas, graphics etc. inside your Flash Movie. It's based on the xpdf PDF parser from Derek B. Noonburg. • SWFCombine A multi-function tool for inserting SWFs into Wrapper SWFs, contatenating SWFs, stacking SWFs or for basic parameter manipulation (e.g. changing size). • SWFStrings Scans SWFs for text data. • SWFDump Prints out various informations about SWFs, like contained images/fonts/sounds, disassembly of contained code as well as cross-reference and bounding box data. • JPEG2SWF Takes one or more JPEG pictures and generates a SWF slideshow from them. Supports motion estimation compression (h.263) for better compression of video sequences. • PNG2SWF Like JPEG2SWF, only for PNGs. • GIF2SWF Converts GIFs to SWF. Also able to handle animated gifs. • WAV2SWF Converts WAV audio files to SWFs, using the L.A.M.E. MP3 encoder library. • AVI2SWF Converts AVI animation files to SWF. It supports Flash MX H.263 compression. Some examples can be found at examples.html. (Notice: this tool is not included anymore in the latest version, as ffmpeg or mencoder do a better job nowadays) • Font2SWF Converts font files (TTF, Type1) to SWF. • SWFBBox Allows to read out, optimize and readjust SWF bounding boxes. • SWFC A tool for creating SWF files from simple script files. Includes support for both ActionScript 2.0 as well as ActionScript 3.0. • SWFExtract Allows to extract Movieclips, Sounds, Images etc. from SWF files. • AS3Compile A standalone ActionScript 3.0 compiler. Mostly compatible with Flex. SWFTools
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值