破晓 发表于 2015-1-8 10:02:35

用Embed引入的swf资源,取出里面的子资源

以前取出用Embed嵌入的资源都用这样的方法:

public var img:Class;然后实例化使用,今天打断点发现用Embed嵌入的资源运行时进入了MovieClipLoaderAsset类,看一下其构造方法:
public function MovieClipLoaderAsset()
{
super();var loaderContext:LoaderContext = new LoaderContext();
loaderContext.applicationDomain = new
   ApplicationDomain(ApplicationDomain.currentDomain);

// in AIR...
// when embedding a SWF using @Embed, you are actively asking for the SWF
// to be executed, otherwise the SWF will fail loading due to
// Loader.allowLoadBytesCodeExecution.
//
// this property prevents accidentally loading a potentially dangerous
// SWF into the application sandbox.
//
// since this property is indirectly accessed, this should be revisited
// after AIR 1.x, as it may become deprecated
if ("allowLoadBytesCodeExecution" in loaderContext)
   loaderContext["allowLoadBytesCodeExecution"] = true;loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE,
            completeHandler);
loader.loadBytes(movieClipData, loaderContext);
addChild(loader);
}
发现添加了一个loader,于是发现只要把loader取出来就可以了
                        
                        public var swf:Class;
                        
                        private function onLoadComplete(event:Event):void
                        {
                                    var cls:Class = event.target.applicationDomain.getDefinition("image1") as Class;
                                    var bmp:BitmapData = new cls();
                        }
                        
                        private function init():void
                        {
                                    var c:* = new swf();
                                    (c.getChildAt(0) as Loader).contentLoaderInfo.addEventListener(Event.COMPLETE, onLoadComplete);
}取出图片成功

不过Embed的方式其实效率并不是太高,而且还增加主工程的大小,所以还是建议用loader来加载资源
页: [1]
查看完整版本: 用Embed引入的swf资源,取出里面的子资源