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);

Hi can you send me the FLA file for this
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
thanks
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)
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.
Mark,
This example was for Flash CS3/CS4/ActionScript 3.0, not for a Flex project.
Peter
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
This way is a little bit faster…
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
Found out the error!!!
My firewall was blocking the request!
thanks man
How would I get this to work where you can click buttons to load these various images, one at a time?