PDA

View Full Version : How can I keep adding and removing the same content to container?



SasuS
May 12th, 2008, 10:32 AM
Hello,
I'm trying to make an order form that would be closed from a button. I know this is simple question, but how the odd thing is that when I press the close button named btn the first time everything works fine. The second time I open the form and close it I get an error message:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild() at MethodInfo-994()

The third time I open and close the form I get the error two times. The fourth time I open and close the form I get the error four time etc.. I think you get the idea... The problem must be somewhere in removing the child and then adding it again and removing. Can someone help me please? Here is the code:


var container:Sprite = new Sprite();
addChild(container);
var forma1:Loader = new Loader();
var urlReq:URLRequest = new URLRequest("file.swf");
forma1.load(urlReq);
forma1.contentLoaderInfo.addEventListener(Event.CO MPLETE, loaded);

function loaded(event:Event):void
{
container.addChild(forma1);
btn.addEventListener(MouseEvent.CLICK, closeforma);
function closeforma(event:MouseEvent):void
{
container.removeChild(forma1); // I also tried removeChild(container) without any better luck....
}
}

amarghosh
May 12th, 2008, 11:06 AM
i think u r not removing anonymous listeners:
try adding:
btn.removeEventListener(event.type, arguments.callee);
after this line:
container.removeChild(forma1);

SasuS
May 13th, 2008, 03:06 AM
Indeed, I had forgot to remove the listener... Thanks a lot amarghosh, that solved my problem!!!