Transversing the Display List Correctly
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.











