一个简单的加载外部SWF文件类

声明:欢迎任何人和组织转载本blog中文章,但必须标记文章原始链接和作者信息。

本文链接:http://blog.csdn.net/li_007/archive/2009/03/28/4032276.aspx

开拓进取的小乌龟------->CSDN点滴点点滴滴Blog

 

紧接着上一篇blog,贴一个我自己平时用的加载AVM2的swf的类,同大家分享。

// // CLoadMedia.as // // you can load external swf and pictures used this class // // Written by Leezhm, 7th Nov, 2008 // Contact : [email protected] // Last Modified by Leezhm on 7th Nov, 2009 // package Srcs { import flash.display.DisplayObjectContainer; import flash.display.Loader; import flash.display.MovieClip; import flash.display.LoaderInfo; import flash.events.Event; import flash.net.URLRequest; public class CLoadMedia { private var _file:String = ""; private var _loader:Loader; private var _request:URLRequest; private var _display:DisplayObjectContainer; private var _currentMC:MovieClip; public static const VIDEO_IS_OVER:String = "videoisover"; public function CLoadMedia(str:String, dis:DisplayObjectContainer) { if ("" != str) { this._file = str; } else { trace("the path of extern file is null!!!"); } if (null != dis) { this._display = dis; } } public function Loading():void { if (null == this._request) { this._request = new URLRequest(this._file); } if (null == this._loader) { this._loader = new Loader(); } this._loader.load(this._request); this._loader.contentLoaderInfo.addEventListener(Event.INIT, OnContentInit); this._display.addChild(this._loader); } public function get CurrentMovieClip():MovieClip { if (null != this._currentMC) { return this._currentMC; } } private function OnContentInit(evt:Event = null):void { this._loader.contentLoaderInfo.removeEventListener(Event.INIT, OnContentInit); if (3 == evt.target.actionScriptVersion) { evt.target.addEventListener(Event.COMPLETE, OnLoadCompleted); } else { trace("the actionscript version of swf file is incorrect!!!"); } } private function OnLoadCompleted(evt:Event = null):void { this._display.visible = true; this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, OnLoadCompleted); this._currentMC = MovieClip(evt.target.content); evt.target.content.addEventListener(Event.ENTER_FRAME, OnLoaderEnterFrame); } private function OnLoaderEnterFrame(evt:Event = null):void { if (MovieClip(evt.target).currentFrame == this._currentMC.totalFrames) { if (null != this._request) { this._request = null; } if (null != this._loader) { this._loader.dispatchEvent(new Event(CLoadMedia.VIDEO_IS_OVER)); this._display.removeChild(this._loader); this._display = null; this._loader.unload(); this._loader = null; } evt.target.addEventListener(Event.ENTER_FRAME, OnLoaderEnterFrame); } } } }

 

在swf文件播放完成后,会发送一个VIDEO_IS_OVER的事件,由于是用_loader发送的,所以必须在capture阶段来捕获这个事件(Actionscript 3的事件流机制)。下面是一个简单的应用:

// // LuxFlashEnvironment.as // // the entry of application // // Written by Leezhm, 7th Nov, 2009 // Contact : [email protected] // Last Modified by Leezhm on 7th Nov, 2009 // package Srcs { import flash.display.Sprite; import flash.events.Event; public class LuxFlashEnvironment extends Sprite { private var _loadMedia:CLoadMedia; public function LuxFlashEnvironment() { if (stage) { InitApplication(); } else { this.addEventListener(Event.ADDED_TO_STAGE, InitApplication); } } private function InitApplication(evt:Event = null):void { this.addEventListener(CLoadMedia.VIDEO_IS_OVER, OnVideoPlayOver, true); this._loadMedia = new CLoadMedia("Medias//Middle.swf", this); this._loadMedia.Loading(); } private function OnVideoPlayOver(evt:Event = null):void { if (null != this._loadMedia) { this._loadMedia = null; } } } }

 其实在CLoadMedia class中我们还实现了一个获得被加载的SWF的转化成MovieClip实例的属性CurrentMovieClip,有了这个属性后,我们可以像操作MovieClip一样来操作被加载的external swf file。

你可能感兴趣的:(function,String,File,null,Class,actionscript)