Skip to main content
Inspiring
February 8, 2008
Question

AS3: Replacing a loaded image..?

  • February 8, 2008
  • 2 replies
  • 907 views
I'm stuck with an button-event that load an image into a container.

( the code: mb1.button.addEventListener(MouseEvent.CLICK,ClickEvent1);
function ClickEvent1(myEvent:MouseEvent):void
{
var pynt:URLRequest = new URLRequest("icon01.png");
var pyntLoader1:Loader = new Loader();
var pyntLoader2:Loader = new Loader();
pyntLoader1.load(pynt);
pyntLoader2.load(pynt);
p1.addChild(pyntLoader1);
p2.addChild(pyntLoader2);
}

)

At this stage there's no problem loading the image.

But when hitting another button created I want the image to be removed from the containers and replaced with a new image ( icon02.png).

It's easy, right? Wrong!!

By using removeChild() i should be able to remove the image in the containers... But it won't work.
At one point it removes the image but I'm unable to load a new image.

Do I also have to remove/unload other functions/events?
I've tried to use unload() but I'm getting an error saying that unload() is not a function...

When learning the basics of coding you need to understand the logic behind the bolts and nuts. With AS3 I get the feeling that the logic within the codes is well hidden. Isn't that like moving in the wrong direction?

Anyway, I really hope I can get some help with this... I'm about to invest the rest of my money in the biggest sledgehammer in the world... And then it's bye, bye my computer... !!
This topic has been closed for replies.

2 replies

Inspiring
February 9, 2008
Thanks...!

That really worked out perfect.
Your answer (not just the code) made me see where I went wrong.

Thanks again!
February 9, 2008
You made a new instance of the Loader class inside the ClickEvent1 function, because of this, you can only access pyntLoader1/2 from within the function (since that's were you created them). So, what you need to do it create the instances of the loader class outside the function. Then, you can easily access them (and their properties/functions) from anywhere. So, to remove the image when you press another button, all you would need to do is write p1.removeChild(pyntLoader1); p2.removeChild(pyntLoader2);. Or, if you just want to change the image, instand of removing it and loading another image into it, you can simply just used the Loader instances you created and load another images into the 'container' movie clip like so: pyntLoader1.load(pynt2);
pyntLoader2.load(pynt2);
If you still can't get it working post all your code using the Attach Code button and I'll take a look at it.