2016-8-30 zhulinan Flash
被加载的swf里构造MyBall类:package { import flash.display.MovieClip; import flash.events.*; public class MyBall extends MovieClip { public function MyBall() { addEventListener(Event.ADDED_TO_STAGE, addedHandle); } private function addedHandle(e:Event):void { trace("MyBall is created") } } }
主文件代码:package { import flash.display.MovieClip; import flash.display.Loader; import flash.display.LoaderInfo; import flash.events.*; import flash.net.URLLoader; import flash.net.URLRequest; import flash.system.ApplicationDomain; import flash.utils.getDefinitionByName; public class Main extends MovieClip { private var _myCls:Class; private var _ballNum:uint = 0; public function Main() { addEventListener(Event.ADDED_TO_STAGE, addedHandle); } private function addedHandle(e:Event):void { removeEventListener(Event.ADDED_TO_STAGE, addedHandle); var loader:Loader = new Loader(); var url:URLRequest = new URLRequest("myBall.swf"); loader.load(url); loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaderCompleteHandle); btnAdd.addEventListener(MouseEvent.CLICK, btnAddClickHandle); } private function loaderCompleteHandle(e:Event):void { var swfTarget:LoaderInfo = e.target as LoaderInfo; var appDomain:ApplicationDomain = swfTarget.applicationDomain as ApplicationDomain; _myCls = appDomain.getDefinition("MyBall") as Class; } private function btnAddClickHandle(e:MouseEvent):void { var mc:MovieClip = (new _myCls()) as MovieClip; mc.x = _ballNum * 30; mc.y = _ballNum * 30; addChild(mc); _ballNum ++; } } }
下载本例