Flash is wonderful. Not only can you load data that is mostly numeric or text, but you can also load assets such as images and even other .swf files. Global loadMovie()The global loadMovie method enables you to load an image (JPG, GIF, or PNG) or .swf file into any target movie clip, and requires the name of the file that will be loaded and a path to the target clip into which the file will be loaded. The image or .swf file replaces any contents of the movie clip and is aligned by its upper-left corner at the registration point (crosshairs) inside the clip. This technique could be useful when you want to load images into different targets. Tip It is usually easiest to create a movie clip with a placeholder rectangle the same size as the .swf or image that you are trying to load. This enables you to visually place it and know where your loaded content will be landing. In the following code, the image.jpg image file is loaded into the target_mc movie clip: loadMovie("image.jpg", target_mc); This code can be used with a button event handler to provide a user interface to a selection of images: myButton.onRelease = function(){ loadMovie("image1.jpg", logo_mc); }; With the global loadMovie method, you can load images and other .swf assets into any target clip in your project. However, the global loadMovie method is just one way to approach loading external assets. The next two sections discuss approaches that are preferred over the global loadMovie method. MovieClip.loadMovie()The loadMovie method of the MovieClip class permits you to dynamically load external assets into a target clip that has been created at run-time. This is useful because it can reduce your project's initial file size and allow for more flexibility in your code. In fact, this method for loading external images and .swf files is preferred over the global loadMovie method. First, create an empty movie clip as the target clip: this.createEmptyMovieClip("target_mc", 100); After you have a target movie clip, you can load an external .swf or image file into it: target_mc.loadMovie(image.jpg); The major drawback of using the loadMovie method is that there is no built-in method to handle the loading progress of external files. MovieClipLoader()The MovieClipLoader class is used to load external resources (such as .swf, .jpeg, .gif, and .png files) and utilize the built-in event handlers of a MovieClipLoader object. It works much like the event handlers in the LoadVars and XML objects. You can either create a new MovieClipLoader object for each file you will be loading or use a single MovieClipLoader object to handle the loading of several files. If you're loading only a few assets into the .swf, you might want to create a new MovieClipLoader for each asset. On the other hand, if you are loading many assets, a single MovieClipLoader can manage them all without stressing the user's processor. In the following example, an empty movie clip is created to hold the incoming image file. Then a new object is created to be a listener for the MovieClipLoader (which is created soon). In the listener function, called mclListener.onLoadInit, the code to be applied to the holder clip is applied after it holds the external image file. Why do this? When an external file is loaded into a movie clip, it overwrites any existing code on that clip (just as it replaces any visuals). So any changes to the clip must be done after the external image or .swf is loaded. When the listener functions are defined, you can create the MovieClipLoader object instance and register the listener. After all this preparation, you can finally load the external image into the holder clip with the MovieClipLoader. // create an empty clip into which the image is loaded this.createEmptyMovieClip("image_mc", this.getNextHighestDepth()); // create an object to use as a listener var mclListener:Object = new Object(); //define a function for the onLoadInit event handler mclListener.onLoadInit = function(target_mc:MovieClip) { // position target clip on stage target_mc._x = Stage.width/2-target_mc._width/2; target_mc._y = Stage.height/2-target_mc._width/2; // resize image to 40% target_mc._xscale = 40; target_mc._yscale = 40; }; // create a new MovieClipLoader var image1_mc:MovieClipLoader = new MovieClipLoader(); //register the listener to the MovieClipLoader image1_mc.addListener(mclListener); // load an image into the target movie clip image1_mc.loadClip("image_url.jpg", image_mc); The advantage of the movieClipLoader is that it can respond to more events related to loading of the data than the other approaches to loading external assets. MovieClipLoader EventsWith the MovieClipLoader, you can listen for various events during the loading process, including the following:
When these events are invoked, you can run code specific to that event. For example, you can use the onLoadProgress event handler to give your end user feedback on how the download is progressing. MovieClipLoader MethodsThere are several methods in the MovieClipLoader class, and they involve the loading and unloading of external assets. Following is a list of methods that can be applied to an instance of the MovieClipLoader.
Now that you know how to load external resources into your Flash application, you need to know something about the security issues involved. |