您的位置:首页 > 其它

关于Actionscript 3加载外部swf

2009-03-27 22:22 204 查看

关于Actionscript 3加载外部swf

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

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

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

对于外部swf的加载,Actionscript 3已经改变了Actionscript 2/1的实现方法,当然Actionscript提供的方法也更友好,有效,更简便控制外部swf文件。比如在加载完成后获得swf文件的帧数,swf文件的一些属性,已经加载过程的信息,etc。当然能够得到这些数据,肯定能够很方便地控制swf的播放,暂停等。在这里多说点,在论坛上看到很多人说通过代码如下代码获取外部swf文件的帧数为1或者无法获取。

this._loader.contentLoaderInfo.addEventListener(Event.COMPLETE, OnLoadCompleted);
private function OnLoadCompleted(evt:Event = null):void
{
this._display.visible = true;

this._loader.contentLoaderInfo.removeEventListener(Event.COMPLETE, OnLoadCompleted);

this._totalFrames = MovieClip(evt.target.content).totalFrames;

evt.target.content.addEventListener(Event.ENTER_FRAME, OnLoaderEnterFrame);
}


其实,在这里我们要注意一点。Actionscript 3中将as2/1的swf转换成了AVM1Movie类型,无法转换成MovieClip,当然是无法获得总帧数,通过LoadInfo的ActionscriptVersion属性可以得到当前被加载的swf的Actionscript的版本(关于Loader、LoaderInfo和URLRequest待会讲),如下图:



图 一、Actionscript 2的swf加载信息截图

再看ActionscriptVersion = 3的swf文件加载调试截图:



图 二、Actionscript 3的swf加载信息截图

好了,现在先说说flash.display.Loader、flash.display.LoaderInfo和flash.net.URLRequest。首先加载外部swf、Pics等显示对象需要用Load,如果是加载xml已经二进制数据的时候,需要用URLLoader。URLRequest是提供加载对象的URL。关于LoaderInfo对象,可以看看Adobe Docs的描述:

The LoaderInfo class provides information about a loaded SWF file or a loaded image file (JPEG, GIF, or PNG). LoaderInfo objects are available for any display object. The information provided includes load progress, the URLs of the loader and loaded content, the number of bytes total for the media, and the nominal height and width of the media.

You can access LoaderInfo objects in two ways:

The
contentLoaderInfo
property of a flash.display.Loader object— The
contentLoaderInfo
property is always available for any Loader object. For a Loader object that has not called the
load()
or
loadBytes()
method, or that has not sufficiently loaded, attempting to access many of the properties of the
contentLoaderInfo
property throws an error.

The
loaderInfo
property of a display object.

The
contentLoaderInfo
property of a Loader object provides information about the content that the Loader object is loading, whereas the
loaderInfo
property of a DisplayObject provides information about the root SWF file for that display object.

When you use a Loader object to load a display object (such as a SWF file or a bitmap), the
loaderInfo
property of the display object is the same as the
contentLoaderInfo
property of the Loader object (
DisplayObject.loaderInfo = Loader.contentLoaderInfo
). Because the instance of the main class of the SWF file has no Loader object, the
loaderInfo
property is the only way to access the LoaderInfo for the instance of the main class of the SWF file.

每个显示对象都有LoaderInfo属性的,它提供了加载对象的高宽度、字节数、加载进度、加载对象的URL以及前面所说的ActionscriptVersion等等信息。对于一个被加载的swf对象,Loader 对象的
contentLoaderInfo
属性提供有关 Loader 对象正在加载的内容的信息,而 DisplayObject 的
loaderInfo
属性提供有关该显示对象的根 SWF 文件的信息。 使用 Loader 对象(SWF 文件或位图)加载的对象的
loaderInfo
属性引用与 Loader 对象的
contentLoaderInfo
属性相同的 LoaderInfo 对象。换句话说,LoaderInfo 对象是加载的对象文件与加载它的 Loader 对象之间的共享对象。由于 SWF 文件的主类的实例没有 Loader 对象,因此
loaderInfo
属性是访问 SWF 文件主类实例的 LoaderInfo 的唯一方法,而Loader对象的Content属性表示被加载的文件的根显示对象,所以通过这些属性可以很容易来访问被加载的对象和加载对象。仔细查看图二中evt.target.loader的content、contenLoaderInfo、LoaderInfo属性的值就 明白这些了(假设你清楚Actionscript 3的事件流机制,知道currentTarget和Target属性)。看到了evt.target.loader.content值后就知道可以得到加载swf的总帧数了。再看如下加载完成后截图:



图三、Actionscript 3版本swf加载完成后调试截图

顺便再啰嗦下,在我们将flv文件导入fla中的时候,有Import to stage和Import Video俩个选择,看似是一样,其实是不一样的,在Import to stage中是直接Import到MainTimeline,而Import Video则是转化成一个MovieClip。再看一张用Import to Stage的swf加载调试截图:



图四、Import to Stage方式导入fla生成的swf文件加载调试截图

PS:最后附上一张Adobe Docs描述AVM1Movie类型的截图:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: