11.5.09

AS3: Dynamically Loading Images in FlashDevelop

Dynamically Loading Images in FlashDevelop with AS3

Dynamically loading images in an AS3 movies allows you to store images seperately from your movie. Depending on where and how your image is stored there may be significant time involved in loading an image. Another method of using images in your movie by embedding them directly into your movie can be found here.

The Set Up:
  • Either upload an image do your own webspace, or find the address to one online to use for testing. The image must be formatted as a gif, jpg, or png.
  • Create a new AS3 project space in FlashDevelop.

Non-blocking image load:

  • ... Non-Blocking?:
    Loading an image without Blocking means that while the image is being loaded, code continues to run. This is a problem if the application demands that the image displays immediately after you call addChild(). However, blocking image loading takes some foresight in AS3, and non-blocking image loading is adequate for many situations. Blocking image loading will be covered in a future tutorial.

The Code:

// entry point
var imgInstance:Loader = new Loader(); // Create a new loader object
imgInstance.load(new URLRequest("http://sites.google.com/site/programmingetc/Home/image.png")); // Begin loading the image from web
addChild(imgInstance); // Display the image
Line By Line:

  1. Lines up with the "//entry point" comment that should be in the default as3 project in FlashDevelop.
  2. Create a loader object. It will be used to load the image, and can also be used as a DisplayObject for displaying the loaded image.
  3. Call the load method, with a URL parameter that points to an image (hosted on my google webspace).
  4. Add the loader instance to the sprite. The image will be displayed when it loads.

.zip source for this project can be found here

1 comment:

  1. Thanks for sharing!

    Using flashdevelop i get the following error running the code in the zip file:

    VerifyError: Error #1053: Illegal override of z in mx.core.BitmapAsset.

    Whats wrong?

    ReplyDelete