Skip to main content
Known Participant
November 16, 2012
Answered

I want to use for next loop to reduce code for doing removechild()'s

  • November 16, 2012
  • 1 reply
  • 791 views

I have working if (addChild(bmp1)) ----removeChild(bmp1). But I need to do it 20 times. I thought I could use a for/next loop, but so far can't seem to get it right.

for (var i:Number=0; i<21; I++)

{

my_bmp.name = "bmp" + 1;

if (addChild(my_bmp))

{

removeChild(my_bmp);

}

}

}

This code passing the compiler, but when I execute it I get "A term is undefined and has no properties". I'm thinking it has something to do with  my_bmp.name? I've tried several things and just can't crack it.

This topic has been closed for replies.
Correct answer Ned Murphy

The name of an object is just a string.  to have the compiler treat it as an object you can use the getChildByName() method or even the bracket notation.

removeChild(this.getChildByname(my_bmp));

OR

removeChild(this[my_bmp]);

I am guessing that you intend to use "bmp"+String(i) instead of "bmp"+1

1 reply

Ned Murphy
Ned MurphyCorrect answer
Legend
November 16, 2012

The name of an object is just a string.  to have the compiler treat it as an object you can use the getChildByName() method or even the bracket notation.

removeChild(this.getChildByname(my_bmp));

OR

removeChild(this[my_bmp]);

I am guessing that you intend to use "bmp"+String(i) instead of "bmp"+1

Known Participant
November 16, 2012

Yes I meant i not 1.

function clearAddchild()

{

for (var i:Number=1; i<10; i++)

{

  my_bmp = "bmp" + i;

  if (addChild(this[my_bmp]))

  {

   removeChild(this[my_bmp]);

  }

}

}

It works.....thanks

Ned Murphy
Legend
November 16, 2012

You're welcome