views:

26

answers:

2

is it possible to get dynamically created children of a display object instance in action script 3 ?

.

example

trace(_view.getChildByName("name"))

= returns name of display object (success)

.

trace(_view.getChildByName("name").getChildByName("name2")

= returns error 1061

.

thanks a lot and have a nice weekend !

+2  A: 

Yes, you can. The problem is that DisplayObjectContainer.getChildByName() returns type DisplayObject, and an arbitrary display object may or may not be a DisplayObjectContainer. So, while you can do that, you first need to cast the type of the result to a DisplayObjectContainer:

public static function getGrandChildByName(
         parent : DisplayObjectContainer,
         child : String,
         grandchild : String
) : DisplayObject {
    var child_obj : DisplayObject = parent.getChildByName(child);
    var child_container : DisplayObjectContainer = child_obj as DisplayObjectContainer;
    return child_container.getChildByName(grandchild);
}

Note that in the example I gave above, I didn't do any checking to verify that the child is actually exists and is a DisplayObjectContainer.... in actual production code, you might want to add such checks.

Also, one last note, if you are using type MovieClip, you can simply refer to the object by its name:

  myclip.mc_child.mc_grandchild.gotoAndStop(3);

Simply refering to the elements by name is simpler and less error-prone. I highly recommend it.

Michael Aaron Safyan
+1 for funky parameter formatting. lol
gmale
@gmale, what do you mean?
Michael Aaron Safyan
@Michael: just being silly. Each of your function parameters are on their own line. That, combined with the space around the ":" give the function signatures "pizzazz!" +1 for pizzazz.
gmale
A: 

While I am sure that Michael Aaron's answer can be usefull... when I need to reference only one or two items, I do it this way:

trace( MovieClip( MovieClip( _view.getChildByName("name") ).getChildByName("name2") ).name );

This is basically just coercing the displayobject into acting like a MovieClip so I can read its name property and use any of the MoveClip methods on it.

If you need to reference an entire display of items, something more complicated could be in order....

You can also use it to access hard-to-reach TextFields.... trace( TextField( MovieClip( _view.getChildByName("name") ).getChildByName("textfieldName") ).text );

exoboy