0

Transversing the Display List Correctly

Transversing the Display List Correctly via @largestartist Posted by Jose Cuadra on December 10, 2009 Tags: Filed Under: References

When coming from JavaScript a common mistake when drilling down through objects is that chaining doesn’t work like you would think in AS3. Yes the languages are similar but here’s a difference.

DOM

In JavaScript you would transverse the DOM using a method called “getElementsByTagName().” You can chain these to dig into the objects.

//This grabs the first input field inside the first div on the page
var textField = document.getElementsByTagName("div")[0].getElementsByTagName("input")[0];

Now in ActionScript you can’t double up on the “getChildAt()” method, because that method doesn’t have a method of “getChildAt().” Confused yet?

In ActionScript 3 you have to cast the method into an object so you can use the method again to drill down.

ActionScript 3

var sprite:Sprite = new Sprite();
var txtField:TextField = new TextField();
txtField.text = "something";
sprite.addChild(txtField);
stage.addChild(sprite);
 
//Cast the object returned to a DisplayObjectContainer 
var path:Sprite = Sprite(stage.getChildAt(1));
 
trace(path.getChildAt(0));//[object TextField]
//trace(stage.getChildAt(1).getChildAt(0));//Error - 1061: Call to possibly undefined method getChildAt through reference with static type flash.displa:DisplayObject.

Want More? Try These.

Related Articles
The Textfield’s Alpha Property in AS 3.0
Flash Slideshow in Actionscript 3
ActionScript 2 to 3 Object Properties
Random Functions
Conditional Shortcut in ActionScript
Random Articles
ActionScript 2 to 3 Object Properties
Target Blank for XHTML
The Definitive List of Adobe Alternatives
Reiq’s Corel Painter Tutorial
Funny Superman Shirts
Flash Slideshow in Actionscript 3
Font Fight!
The Coolest Lightboxes Around
CFCACHE, IE, CSS and XHTML
Classy Glassy Orbs

Leave a Reply