Skip to main content
Inspiring
November 17, 2008
Question

Removing Items From Stage Using removeChild

  • November 17, 2008
  • 5 replies
  • 742 views
So, I have a file where I've added 27 movie clips to the stage using a for loop to cycle through items in an XML document. Here's the pertinent part of the code I'm using, it's AS3:


So, I end up with 27 instances of mini_mc, each named 0, 1, 2, 3, etc.

I'm trying to write code to remove them at a certain point in the movie but having problems. Here's what I'm using:


But I get this error:

ReferenceError: Error #1069: Property 0 not found on String and there is no default value.

I'm thinking I'm not referencing the "name" of the mcs properly?

Any help would be greatly appreciated! Thanks very much.
This topic has been closed for replies.

5 replies

Ned Murphy
Legend
November 18, 2008
Struggle with it... that's what I do. It's the best (maybe the only) way to learn this stuff. AS3 gets easier once you start getting things under control.
backpagesAuthor
Inspiring
November 18, 2008
Thanks so much, Ned. I'm making progress. Still not there yet, but getting close. I'm now using this to name the movie clips I create dynamically:

mini_mc = new mcMini();
mini_mc.name = "celebrity" + String(i);

I had declared a variable for mini_mc earlier in the code like this:

var mini_mc:mcMini;

Anyway, I'm working on trying to understand all this and get it working. Thanks again for your help.
Ned Murphy
Legend
November 18, 2008
Assigning names: yes, except i is an intener, so it would have to be "mini_mc"+String(i)

name is a property: yes, but no, if the name could be the number represented by i, it would be mini_mc.1 etc...

object names are no longer what they used to be for dynamically created objects... you cannot access them that way anymore in AS3. To use the object name to manipulate an object you have to first target the object by its name.... var target:Object = getChildByName(theName); and then use the target to manipulate it... removeChild(target);

error 1067: you're using an integer "i" to try to name something... it won't fly unless you convert it to a string

Also, when you decalre a variable, you need to specify its type/class...for example...

var i:uint = 0

var mini_mc:mcMini = new mcMini();


backpagesAuthor
Inspiring
November 17, 2008
Ned:

Thanks so much for the quick reply. I double checked and I DO have Strict turned on, so not sure about that.

So would I be better off assigning the names like this so they don't start with a number: mini_mc.name = "mini_mc" + i;

And since name is a property, not a function should it be written like this: stage.removeChild(mini_mc.name.i)

I tried your last suggestion: stage.removeChild(numChildren-1); but it won't run, it throws this error: 1067: Implicit coercion of a value of type int to an unrelated type flash.display:DisplayObject.
Ned Murphy
Legend
November 17, 2008
I know it doesn't fly for variable names, so it may not fly for object names as well.... you can't use numbers to start a name. And name is a property, not a function (). And you must have the strict mode turned off because it wouldn't let a number be assigned to a String type... and a few other things you've coded.

Aside from that, you need to target the object using its name.

var theMC:Object = getChildByName(name);
removeChild(theMC).

Now, if these objects were the last things added to the stage, then you wouldn't have to worry about their names.

for(var i = 0; i < myXML.photo.length(); i++) {
stage.removeChildAt(numChildren-1);
}