Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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
}
Copy link to clipboard
Copied
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
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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.
Copy link to clipboard
Copied
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);
}
Find more inspiration, events, and resources on the new Adobe Community
Explore Now