6、我们已经为onLoadStart事件部署了相应的工作,接下来我们要为上述其他事件部署工作了。紧接着是onLoadProgress,它接受三个参数:targetMC, loadedBytes, totalBytes。分别代表目标容器MC实例;已经读取的体积、总体积。
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、我们的onLoadComplete方法仅接受一个参数,它就是容器MC实例。像onLoadStart一样,我们用getProgress方法来返回读取情况。
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、onLoadInit方法将在所有加载的内容被下载到本地容器MC中之后才开始执行。这将使得你能更好的控制加载进来的内容的属性。我选择的图片非常大,这样我们可以把读取过程看得更加清晰,而我也要对已经加载的图片尺寸进行修整,让它能全部显示出来。
myMCL.onLoadInit = function (targetMC) { myTrace ("Movie clip:" + targetMC + " is now initialized"); targetMC._width = 170; targetMC._height = 170; }
9、还有一个回调方法onLoadError。如果有错误发生,它将会被触发。作为一个优秀的程序员,部署完善的应用程序的时候,对错误发生的避免措施是必不可少的!
myMCL.onLoadError = function (targetMC, errorCode) { myTrace ("ERRORCODE:" + errorCode); myTrace (targetMC + "Failed to load its content"); }
10. Well that's the hard work out of the way. Now we just have to load the files in to their respective targets, using loadClip, and passing it two arguments: the location of your file, and the destination movieclip for the file to load in to.
10、我们终于将最复杂的工作部署好了。接下来我们只用使用loadClip方法读入我们需要的内容就行了。loadClip方法的两个参数分别是外埠资源的地址和容器MC的实例。
路径可以选择相对路径。注意,路径的相对性也是一个大问题,当SWF在非本路径的HTML中被引用的时候,遵从HTML所在的路径!这一点是很多Flash教程都忽视的。所以,有时候绝对路径也有绝对路径的好处。[路径问题源文件下载,下载了就一目了然了
所有的调试工作最好在浏览器中,而非IDE中完成。而且脚本输出方式必须是AS2。 Remember, for everything to work properly you need to be testing throuhg a browser (and preferably on line so you can see the files loading in real time). You also need to be exporting your code as ActionScript 2.
In the second part of this tutorial I'm going to show you how to use the MovieClipLoader class in a real-world situation, in order to solve a common problem when assigning event handlers to MovieClips dynamically. 接下来,我将介绍实时调用MCL的情况。为了能适应更多的应用,我们经常动态地为MCL制定工作。
aw话外音:有时候,我们如此写:
1、var mcl:MovieClipLoader = new MovieClipLoader (); 2、var mcl = new MovieClipLoader ();
发现第一种写法无法为MCL制定onLoadStart等事件方法。这是编译器根据指定变量的数据类型产生的问题。osflash的一些朋友给了一些有用的观点,我也发现这个问题正好涉及到Flash内部的事件响应机制,不妨介绍一下: Flash的三种事件响应机制
1、简单的回调函数,最老的; 2、侦听器,ASBroadcaster,FlashMX时代; 3、事件侦听器,EventDispather,FlashMX2004时代
这里,MCL用的是第二种机制,而整套V2组件则使用最后一套机制。 附:MCL官方申明,注意:上述方法中,仅包含getProgress方法!
个人补充认为:1、2在不严格要求数据类型的时候可以通用。
下面开始介绍用侦听器来检测MCL事件的方法。在此之前,我们解决一个最常见的问题,我们经常会在论坛中看到有人这样提问:
引用 :
大家好,我动态地建立了一些MC,并逐个分配给它们一个事件句柄(标志)。然后,我将外埠资源读取到它们之中。但是这些分配好的事件句柄都不工作了! |