關於Actionscript 3加載外部swf

              關於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或者無法獲取。

其實,在這裏我們要注意一點。Actionscript 3中將as2/1的swf轉換成了AVM1Movie類型,無法轉換成MovieClip,當然是無法獲得總幀數,通過LoadInfo的ActionscriptVersion屬性可以得到當前被加載的swf的Actionscript的版本(關於Loader、LoaderInfo和URLRequest待會講),如下圖:

ActionscriptVersion = 2

                                                                        圖 一、Actionscript 2的swf加載信息截圖

再看ActionscriptVersion = 3的swf文件加載調試截圖:

ActionscriptVersion = 3

                                                                  圖 二、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的總幀數了。再看如下加載完成後截圖:

LoadComplete

                                         圖三、Actionscript 3版本swf加載完成後調試截圖

順便再囉嗦下,在我們將flv文件導入fla中的時候,有Import to stage和Import Video倆個選擇,看似是一樣,其實是不一樣的,在Import to stage中是直接Import到MainTimeline,而Import Video則是轉化成一個MovieClip。再看一張用Import to Stage的swf加載調試截圖:

MainTimeline

             圖四、Import to Stage方式導入fla生成的swf文件加載調試截圖

 

PS:最後附上一張Adobe Docs描述AVM1Movie類型的截圖:

AVM1Movie in Actionscript 3

<script src="http://s.vdoing.com/u/107/54977.js" type="text/javascript"></script>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章