Thursday, August 20, 2009

Load an image with ActionScript(AS2 and AS3)

Create a new flash file and save it in the same folder the has the image you want to load. Let the image be named as myImage.jpg.
Load with AS2 ->
Copy and paste the following code:


_root.createEmptyMovieClip("image_mc",_root.getNextHighestDepth());
image_mc.loadMovie("myImage.jpg");


The first line creates a movie clip instance "image_mc". _root.getHighestDepth() function returns the next highest depth such that "image_mc" is sttached on top of any other symbols that may be present on Stage. Instead of this can use numerical values such as 0, 1, 52168, etc. The second line loads the image.

Load with AS3->
Copy and paste the following code:

var myLoader:Loader = new Loader();
myLoader.load(new URLRequest("myImage.jpg"));
addChild(myLoader);

This is a lot different from AS2. Here the image is not loaded by a MovieClip instance but by an object, "myLoader", of the new Loader class . The third line actually attaches the image to the Stage. You might notice there was no such function in AS2.

No comments:

Post a Comment