Saturday, July 17, 2010

The Loader() Class


I've been working on a project with Koolmoves which eventually will display images in an Online Gallery. ActionScript 3 does this with the Loader() class. This class is designed to load external images and movies and display them on screen. This blog post begins to demonstrate the loader class for those just starting out.
The first thing you will need to do is prepare a JPEG file and for convenience sake name it "image" and save it to a folder which you can name, again for convenience sake, "loaderClass". Open a new KoolmovesFlash version there. Save your fun file in this loaderClass folder as "loaderSample".
Now open the ActionScript editor (make sure ActionScript 3 is showing at the bottom)and we will use the contructor method of the loader class to define a new instance of this loader class. We'll call this new instance "imageLoader" and use the addChild method to add it to the stage. You always have to use addChild() for anything to show up visually when you play your movie.

var imageLoader:Loader = new Loader();
addChild(imageLoader); 
At this point we have an empty Loader object on the stage called imageLoader. Now we have to call the load method to bring in our image. So we call this method by writing it thus:

imageLoader.load(new URLRequest("image.jpg"));
 
Your code then should look something like this.

You may have to save your file again in order to get results like I did, but you should have your image show up in the upper left hand corner of your movie. That is about how simple it gets with this class. Hopefully we can continue exploring this class as time permits. You can also position your image by manipulating the imageLoader like this in order to re-position it on the stage.
Your image should move 30 pixels to the right and 30 pixels down.

No comments:

Post a Comment