Skip to main content
Participating Frequently
September 18, 2012
Question

How to use multiple removechild

  • September 18, 2012
  • 1 reply
  • 878 views

i tried to remove 3 images with different removeChild. it doesnt work. it will only clear 1 image.

-----------------------------

stop();

aa1.addEventListener(MouseEvent.CLICK, ona1Click);

bb1.addEventListener(MouseEvent.CLICK, onb1Click);

cc1.addEventListener(MouseEvent.CLICK, onc1Click);

next1.addEventListener(MouseEvent.CLICK, onNext1Click);

home1.addEventListener(MouseEvent.CLICK, onHome1Click);

var Q1a:Loader = new Loader();

var Q1b:Loader = new Loader();

var Q1c:Loader = new Loader();

var Q1next:Loader = new Loader();

var Q1home:Loader = new Loader();

//the name of the function has to match what you're calling from the event listener

function ona1Click(evt:MouseEvent):void {

var myQ1a:URLRequest=new URLRequest("wrong.png");

Q1a.load(myQ1a);

addChild(Q1a);

Q1a.x=320;

Q1a.y=180;

}

function onb1Click(evt:MouseEvent):void {

var myQ1b:URLRequest=new URLRequest("tick.png");

Q1b.load(myQ1b);

addChild(Q1b);

Q1b.x=320;

Q1b.y=260;

}

function onc1Click(evt:MouseEvent):void {

var myQ1c:URLRequest=new URLRequest("wrong.png");

Q1c.load(myQ1c);

addChild(Q1c);

Q1c.x=320;

Q1c.y=340;

}

next1.addEventListener(MouseEvent.CLICK, onNext1Click);

//the name of the function has to match what you're calling from the event listener

function onNext1Click(evt:MouseEvent):void {

gotoAndPlay(2);

removeChild(Q1a);

removeChild(Q1b);

  }

home1.addEventListener(MouseEvent.CLICK, onHome1Click);

//the name of the function has to match what you're calling from the event listener

function onHome1Click(evt:MouseEvent):void {

gotoAndPlay(17);

}

This topic has been closed for replies.

1 reply

Ned Murphy
Legend
September 18, 2012

The approach to removing them as you show should work, though you only show an attempt to remove two of them, not three.  I notice you have duplicated the assignment of the next1 and home1 event listeners, so one set should be removed.

Are you making sure to add all three of them before you click the next1 button?

Are you getting any error messages?  If so, that might be why you only see one getting removed.  Even an unrelated error will bring any other processing to a halt.

naha_ksAuthor
Participating Frequently
September 18, 2012

i have tried to remove the 3 images, but it didn'twork. if i only remove 2, it works just fine.

function onNext1Click(evt:MouseEvent):void {

removeChild(Q1a);

removeChild(Q1b);

removeChild(Q1c);

gotoAndPlay(2);

}

The error of having 3 removeChild :

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.

          at flash.display::DisplayObjectContainer/removeChild()

          at MCQbutton1209d_fla::MainTimeline/onNext1Click()

Ned Murphy
Legend
September 22, 2012

Then you need to determine why the third one is not a child.  Put a trace in your onc1Click function to see if it ever gets executed.  If it doesn't, then that would be a good reason for Q1c to not be a child.