MovieClipLoader 介绍
监听器提供了很多的load 状态 响应,我们来看一看:
・MovieClipLoader.onLoadStart() - invoked when loading begins.
・MovieClipLoader.onLoadProgress() - invoked as the loading process progresses.
・MovieClipLoader.getProgress() - used to obtain the progress of the downloaded file(s).
・MovieClipLoader.onLoadInit() - invoked after the actions in first frame of clip have executed.
・MovieClipLoader.onLoadComplete() - invoked when the entire downloaded file has been written to disk.
・MovieClipLoader.onLoadError() - invoked if the clip cannot be loaded.
・MovieClipLoader.unloadClip() - remove movies/images loaded with this method or cancels a load operation in progress.
可以发现和MX比较,有更多的状态响应。我们从一个简单的例子来入手,了解一下它是如何实现的 。我们要做的是把一系列图片载入一个mc里。
1.新建一影片并在主时间轴第一frame上加AS:
function myTrace(msg) { _root.traceBox.text += msg + newline; _root.traceBox.vPosition+=3;//滚动幅度 _root.traceBox.vScrollPolicy ="on";//确定滚动条为“开”的状态 }
显然我们创建的一个trace函数,在发布的时候把一些信息显示在文本组件里。
2.然后拉进来一个文本组件,把文本框拉大,确保它能显示所有内容。并命名为 “traceBox”
3.创建一MC,在场景里拉进来3个(同一MC)。并把他们分别命名为:"myMC1", "myMC2" and "myMC3". 我们将把图片和swf载入到他们里面。大小为200象素,尽管我们要载入的图片比这大,但我们会在load的时候,重新定义图片的大小。
4.接下来要做的就是创建我们的MovieClipLoader object ,把下面的代码加到第一frame:
var myMCL = new MovieClipLoader();
5.第一帧代码继续:
myMCL.onLoadStart = function (targetMC) { var loadProgress = myMCL.getProgress(targetMC); myTrace ("The movieclip " + targetMC + " has started loading"); myTrace("Bytes loaded at start=" + loadProgress.bytesLoaded); myTrace("Total bytes loaded at start=" +oadProgress.bytesTotal); }
getProgress 属性将得到我们的目标MC(被装载对象)的载入信息:它返回两个参数――――――bytesLoaded and bytesTotal。
这两个参数应该很熟悉的 MM把他们整合到了一起。得到进程-----getProgress
6.仍然是第一frame上,我们给mytrace 文本返回一些信息:
myMCL.onLoadProgress = function (targetMC,loadedBytes,totalBytes) { myTrace ("movie clip: " + targetMC); myTrace("Bytes loaded at progress callback=" + loadedBytes); myTrace("Bytes total at progress callback=" + totalBytes); }
7.继续:
myMCL.onLoadComplete = function (targetMC) { var loadProgress = myMCL.getProgress(targetMC); myTrace (targetMC + " has finished loading."); myTrace("Bytes loaded at end=" + loadProgress.bytesLoaded); myTrace("Bytes total at end=" + loadProgress.bytesTotal); }
8.MX里的装载完毕的位置,大小等的定义总是麻烦,爱出毛病。
下面这个很GOOD。它是在完全装载进来的时候响应的:
myMCL.onLoadInit = function (targetMC) { myTrace ("Movie clip:" + targetMC + " is now initialized"); targetMC._width = 170; targetMC._height = 170; }
9.纠错信息。如果我们要装载的对象无法载入呢?比方URL地址不对?
myMCL.onLoadError = function (targetMC, errorCode) { myTrace ("ERRORCODE:" + errorCode); myTrace (targetMC + "Failed to load its content"); }
10.好了,一切都准备完毕。开始装载我们的对象吧:
myMCL.loadClip("http://www.yourdomain.com/test1.swf","_root.myMC1"); myMCL.loadClip("http://www.yourdomain.com/test2.swf ", "_root.myMC2"); myMCL.loadClip("http://www.yourdomain.com/somepic.jpg", "_level0.myMC3");
多么的惬意。。。
最后,你可以在不需要的时候卸载对象:
myMCL.unloadClip(target);//target specified as a string or integer
下面快要结束了,看一看别的装载方式吧:
myMCL.loadClip("http://www.yourdomain.com/test1.swf","_root.myMC1"); myMCL.loadClip("http://www.yourdomain.com/test1.swf","_level0.myMC1"); myMCL.loadClip("http://www.yourdomain.com/test1.swf", 1);//loads into level 1 myMCL.loadClip("http://www.yourdomain.com/test1.swf", _level0.myMC1); myMCL.loadClip("file:///C:/foldername/images/somepic.jpg", "_level0.myMC4")
- 上一篇: Flash与后台编码问题解决新方案
- 下一篇: AS1.0的OOP 与 AS2.0的OOP对比