Dynamically loading an image in ActionScript 2.0 and ActionScript 3.0

by Peter deHaan on March 2, 2008

in Loader, MovieClip

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… read them below or add one }

1 aditya 08.26.08 at 3:08 am

Hi can you send me the FLA file for this

2 tremain 01.13.09 at 9:10 am

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 01.21.09 at 2:56 am

thanks

4 Pokey 02.23.09 at 1:32 pm

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 02.27.09 at 3:19 am

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 03.09.09 at 8:05 am

Mark,

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

Peter

7 roshan 04.22.09 at 9:50 pm

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 05.27.09 at 3:18 am

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 06.16.09 at 9:17 am

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 06.16.09 at 9:19 am

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

11 vanzo 06.23.09 at 5:13 am

thanks man

12 Amber 07.11.09 at 1:39 pm

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

Leave a Comment

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">

Previous post:

Next post: