Skip to main content
Inspiring
July 21, 2011
Answered

The supplied DisplayObject must be a child of the caller plz help!

  • July 21, 2011
  • 3 replies
  • 1460 views

Hi hope someone can help me with this.

Im adding child to a  MovieClip bg

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

for (var i:int =1; i<=cols; i++)

{

for (var q:int = 1; q<=rows; q++)

{

var random_card = Math.floor(Math.random() * colordeck.length);

tile = new colors();

tile.width = widthvar;

tile.height = widthvar;

tile.col = colordeck[random_card];

colordeck.splice(random_card,1);

tile.gotoAndStop(9);

tile.x =(i+.2)*xvar;

tile.y = (q+adjust)*yvar;

tile.addEventListener(MouseEvent.CLICK,tile_clicked);

bg.addChild(tile);

}

}

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

When I removeChild(bg) it doesnt remove its children and ones I addChild(bg) again

it adds more children on top.

How can i remove these from bg?

I tried:

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

if (bg.contains(tile))

{

for (var i:int =1; i<=cols; i++)

{

for (var q:int = 1; q<=rows; q++)

{

bg.removeChild(tile);

}

}

}

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

but get an error:

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

at flash.display::DisplayObjectContainer/removeChild()

if you can help id rly appriciate it

thx pavel

This topic has been closed for replies.
Correct answer relaxatraja

You can also use some of the methods like:

for (i=mc.numChildren-1;i>=0;i--){

      trace(mc.getChildAt(i).name);

      mc.removeChildAt(i);

}

3 replies

pa-pavelAuthor
Inspiring
July 22, 2011

Thx for the help got it working now

July 21, 2011

>>

if (bg.contains(tile))

{

for (var i:int =1; i<=cols; i++)

{

for (var q:int = 1; q<=rows; q++)

{

bg.removeChild(tile);

<<

You can't do it like that - tile was a local variable when you added it  - ie there's not multiple tile instances so using a for loop is wrong... Either you need to store each tile in an array for later removal, or just clear the container using:

while(bg.numChildren){

     bg.removeChildAt(0);

}

HTH

relaxatraja
Inspiring
July 21, 2011

Whether the bg is added by using addChild(bg) or else on the stage?

pa-pavelAuthor
Inspiring
July 21, 2011

im adding bg by addChild(bg) to another movieclip

relaxatraja
relaxatrajaCorrect answer
Inspiring
July 21, 2011

You can also use some of the methods like:

for (i=mc.numChildren-1;i>=0;i--){

      trace(mc.getChildAt(i).name);

      mc.removeChildAt(i);

}