Skip to main content
Known Participant
January 28, 2013
Question

Movie Clip - load image

  • January 28, 2013
  • 1 reply
  • 966 views

Hi,

I have a function when a button is clicked an image is loaded into the movie clip:

          var imageLoader:Loader;

          function loadImage(url:String):void {

                    imageLoader = new Loader();

                    imageLoader.load(new URLRequest(url));

                    imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageLoading);

                    imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);

          }

          loadImage("images/" + pic + ".jpg");

 

          function imageLoaded(e:Event):void {

  movieclip_mc.addChild(imageLoader);

          }

Is there a way I can remove the previous image from the movie clip before loading the next one?  - so replacing the image with the new one when clicked. (the movie clip starts of empty)

Hope some one can help.

Looking forward to hearing from you.

Many thanks

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
January 28, 2013

If you use the same Loader rather than creating a new one you can just unload that loader using the unload() method.  You can create the Loader outside the function and even addChild() it to the movieclip before anything else.

         var imageLoader:Loader = new Loader(); 

         movieclip_mc.addChild(imageLoader);

          function loadImage(url:String):void {

                    imageLoader.load(new URLRequest(url));

                    imageLoader.contentLoaderInfo.addEventListener(ProgressEvent.PROGRESS, imageLoading);

                    imageLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);

          }

          loadImage("images/" + pic + ".jpg");

 

          function imageLoaded(e:Event):void {

                     // use this for something else or eliminate it

            }

dips045Author
Known Participant
January 28, 2013

Hi Ned

Many thanks for the reply.

Would the unload() method go before the loadImage?

I tried to use the unload() method in my version but no images show up. I used "trace" and it recognises what button I have clicked - but no image appears.

Thanks again for your help

Ned Murphy
Legend
January 28, 2013

The unload would be placed wherever you intend to have the loaded image removed.  So if clicking something is what triggers the chage of images, then the first line of that function should probably be the unload() call.