Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Movie Clip - load image

New Here ,
Jan 28, 2013 Jan 28, 2013

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

TOPICS
ActionScript
923
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 28, 2013 Jan 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

            }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 28, 2013 Jan 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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jan 28, 2013 Jan 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.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 30, 2013 Jan 30, 2013

Hi Ned,

Thanks for your reply. I've been trying to use the unload() method - I can't get it to work.

The image is removed all together so no image shows up.

Any chance you can give me more guidance on this please?

Thanks again.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Jan 31, 2013 Jan 31, 2013
LATEST

for anyone struggling with the same issue, heres the solution

function imageLoaded(e:Event):void {

                    if (movieclip.loadedImage) {

movieclip.removeChild( movieclip.loadedImage );

                    }

                    movieclip_mc.loadedImage=imageLoader;

                    movieclip_mc.addChild(imageLoader);

          }

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines