2

I think of 2 approaches referring to some visual element like movieclip. So suppose i have b_mc ( name="b") lying inside a_mc on the stage : 1st approach :

var mc:MovieClip = a_mc.b_mc

2nd approach :

var mc:MovieClip = a_mc.getChildByName("b")

I generally use 1st approach. What can be good reason to go for 2nd approach. It sometimes seems useless to me, as it involves extra overhead to name the movieclips, when used dynamically.

Thanks V.

1
  • I'd say getChildByName() is unneeded, even if adding MCs dynamically. Usually you will have a more structurized way to refer to MC's children, if there are a lot of them, say by an array, and if there are only a few, reaching via references is easier to understand and debug.
    – Vesper
    Dec 12, 2012 at 14:08

3 Answers 3

3

a.getChildByName('b') is slower than getting a.b.
If you don't need to use names it makes no sense to use it, however some programmers might make use of the name, especially when generating content dynamically rather than through the Flash IDE, in which case having this function is helpful, so that the display list doesn't have to be traversed by a custom function (which it does with internal function anyway, ergo it is slower than a.b)

3

getChildByName is needed if you add something dynamically to a movieclip. For instance:

var mc1:MovieClip = new MovieClip();
var mc2:MovieClip = new MovieClip();
mc2.name = "foobar";
mc1.addChild(mc2);

trace(mc1.foobar); //undefined
trace(mc1["foobar"]); //undefined
trace(mc1.getChildByName("foobar")); //[object MovieClip]

I usually do mc1["foobar"] when referencing objects since that way it's easy to work with items such as mc1["foobar_" + i];

I rarely never use getChildByName since that requires a manual cast whenever you try to pass something as a movieclip/textfield or whatever :)

However, if you do create the instances from the flash editor, then I would usually reference them through some exported class and access them through mc1.mc2.mc3.optionsView.visible

2
  • Sorry, but I am doubtful. Instead of trace(mc1.getChildByName("foobar")); i could directly use trace(mc2). So why go for naming the movieclip and then using getChildByName, in this particular case you mentioned above. :)
    – Vishwas
    Dec 15, 2012 at 14:36
  • Your question revolved around having a child with a "known name" that you needed to access through its' parent. I merely showcased that you can't use the "." or "[]"-operator in that case and have to use getChildByName. Maybe a more "clean" example would have been "given that you somewhere in the code add a child named foobar to a movieclip. And that you then later only have access to the parent in some other part of the code, then the following happens when trying to access the child through its' parent". My bad, I thought it would be easier to understand this example than that text Dec 15, 2012 at 16:00
1

If you have a list of objects that you need to go through programmatically, getChildByName is great. It would assume you've also named them in some programmatic way to reindex them in the for loop. I can also think of a couple other good ways to use this, like pushing the names or objects in an array, so they wouldn't have to be related, just all have some property or method in common.

for(var i:int = 0;i<numOfMC;i++) {
    theParent.getChildByName("child"+i).doSomething();
}
2
  • that's a very inefficient way of doing it because you're doubling the loops, instead looping through for (i = 0; i<mc.numChildren; i++){trace(mc.getChildAt(i));} works better, and guarantees that you don't miss an object that shares a name with another, in which case it would only run on the first instance twice
    – Daniel
    Dec 12, 2012 at 17:27
  • but that also assumes that all the children are things you want to touch - regardless how you do it, what matters more is how you manage your assets. I don't use the Flash Pro IDE if I can help it and stick with everything in FlashBuilder. I've used all of these in the past, it really just matters what works best for you and the project.
    – Gone3d
    Dec 12, 2012 at 18:29

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.