Skip to content
Mar 2 / Peter deHaan

Dynamically loading an image in ActionScript 2.0 and ActionScript 3.0

The following examples show how you can dynamically load an image in ActionScript 2.0 and ActionScript 3.0 using the MovieClip class and Loader class.

Full code after the jump.

The following example shows how you can dynamically load an image using the createEmptyMovieClip() and loadMovie() methods in ActionScript 2.0:

// ActionScript 2.0
var url:String = "http://www.helpexamples.com/flash/images/image2.jpg";
var movieClip:MovieClip = createEmptyMovieClip("movieClip", 0);
movieClip.loadMovie(url);

The following example shows how you can dynamically load an image using the Loader class in ActionScript 3.0:

// ActionScript 3.0
var url:String = "http://www.helpexamples.com/flash/images/image2.jpg";
var urlRequest:URLRequest = new URLRequest(url);
var loader:Loader = new Loader();
loader.load(urlRequest);
addChild(loader);

12 Comments

leave a comment
  1. aditya / Aug 26 2008

    Hi can you send me the FLA file for this

  2. tremain / Jan 13 2009

    hey,

    this tuturoal is really helpful thank you.
    One question – how do you add more than one image using this AS?
    I copyed the code and pasted below but its not working for some reason?

    thanks

    Tremain

  3. isik / Jan 21 2009

    thanks

  4. Pokey / Feb 23 2009

    Thank you for the easy guide ^_^
    it also seems that you can change the image whenever you want by redoing the
    movieClip.loadMovie(url);
    line and giving a different url without having to redo the whole lot XD
    (probibly obvious but thought i would say it anyway)

  5. Mark / Feb 27 2009

    Useless example!

    Running the Example code for ActionScript 3.0 results in an error:
    TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::Loader@1946e6a1 to mx.core.IUIComponent.

    You can’t display the content of a Loader that easy.

  6. Peter deHaan / Mar 9 2009

    Mark,

    This example was for Flash CS3/CS4/ActionScript 3.0, not for a Flex project.

    Peter

  7. roshan / Apr 22 2009

    Great work. Simple. There is one doubt though. I get an error when I execute the code ..

    Error opening URL ‘http://www.helpexamples.com/flash/images/image2.jpg’
    Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.

    I replaced the URL with a file on my system, then it works fine. I tried replacing your link with another link from my website, but it gave the same error. What might be the reason? Im suspecting a security sandbox issue, sometimes links dont work in Flash due to the network settings here. Or is it something to do with the code?

    Regards and Thanks,
    Roshan

  8. Martijn / May 27 2009

    This way is a little bit faster…

    // ActionScript 3.0
    var loader:Loader = new Loader();
    loader.load(new URLRequest("http://www.adobe.com/devnet/images/160x160/flash_cs3_logo.jpg"));
    addChild(loader);
  9. Alex / Jun 16 2009

    Everytime I try to use this script I get the following error:
    Error opening URL '%SITE PATH%'
    Error #2044: Unhandled IOErrorEvent:. text=Error #2035: URL Not Found.

    I’ve tried with many different site paths (all with correct URLs I even tried with the two in this tutorial!) but nothing seems to work :(

    please help!
    - Alex

  10. Alex / Jun 16 2009

    Found out the error!!!
    My firewall was blocking the request!

  11. vanzo / Jun 23 2009

    thanks man

  12. Amber / Jul 11 2009

    How would I get this to work where you can click buttons to load these various images, one at a time?

Leave a Comment