Flash MX 2004 introduces MovieClipLoader(next)

      Flash 2004-8-17 13:57
var myMCL = new MovieClipLoader();//create an instance of MovieClipLoader
5. Okay with that done we're now going to assign our first bit of functionality to our MCL object. If you remember, the onLoadStart method of our class is invoked when loading begins. onLoadStart() receives, as an argument, the name of the movieclip instance calling it. Add the following code to frame 1:
-------------------------
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=" + loadProgress.bytesTotal);
}
------------------------------
The first line of code inside our function declares a new variable "loadProgress", to which we assign the results of our getProgress method (getProgress returns an object with two integer properties: bytesLoaded and bytesTotal). The last two traces send these values (bytesLoaded and bytesTotal) to be displayed in our TextArea component.
6. We have already assigned functionality to our onLoadStart() method. Now we will work through the remaining methods that I listed earlier, and see how we assign functionality to those too. Next up is onLoadProgress() which accepts three arguments (the movieclip in question and its byte-loaded details). Again, we send those details to be displayed in our trace function. Add the following code to frame 1:
-----------------------------------------
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. Our onLoadComplete method accepts one argument which is the name of the clip. Again, we assign the results of getProgress to a variable and extract the byte details for display. 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. The onLoadInit function is not executed until the content has fully loaded in to the movieclip. This makes it the perfect place to put any code which will affect your movieclip's properties. I chose pictures which were quite large for testing purposes, so that we can track the loading progress in the trace box, but we are now going to resize each movieclip so that all the content fits on screen. myMCL.onLoadInit = function (targetMC)
{
myTrace ("Movie clip:" + targetMC + " is now initialized");
targetMC._width = 170;
targetMC._height = 170;
}

9. One of the callbacks we have access to is onLoadError. If an error occurs it returns one of three error messages as a string: "URLNotFound", "LoadNeverCompleted" or "Call Yourself a Programmer?" Okay, make that two. 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. //load the files in to their respective targets
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/pic.jpg", "_level0.myMC3");

Right, change those paths to make them correct for you, publish the file and upload everything to the same directory on your server. I've used absolute paths here but of course relative paths are better used where possible, and if our pictures/swfs are in the same folder as the main swf we would be more likely to type something like: //load the files in to their respective targets
myMCL.loadClip("test1.swf","myMC1");

Now check the results of your hard work in your browser...
--------------------------------------------
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}