Stein G. Strindhaug
Stein G. Strindhaug

Reputation: 5119

From AS2 to AS3 what is the equvalent of _root?

I'm trying to learn Actionscript 2 or 3, with AS2 I eventually figured by trial and error that I could get any named instance and modify it using a string with its name using

var theinstance = "titletext"; // actually exctracted from an array
_root[theinstance].htmlText = "New text with <b>HTML!</b>";

but when trying to convert the code to AS3 _root doesn't exist anymore. According to the migration doc it is somehow replaced by flash.display.DisplayObject.stage but apparently this is not how to do it:

flash.display.DisplayObject.stage[theinstance].htmlText = "New text with <b>HTML!</b>";

and neither is this:

flash.display.DisplayObject.stage.getChildByName(theinstance).htmlText = "New text with <b>HTML!</b>";

How do I get a child by name in actionscript 3?

Upvotes: 2

Views: 8127

Answers (2)

Iain
Iain

Reputation: 9442

Just use either "root" (no underscore) or "stage" depending on exactly what you want to do.

However - Why not just store a reference to the textField in the array instead of a string?

Upvotes: 3

Matt Howell
Matt Howell

Reputation: 15946

"flash.display.DisplayObject" is not literally part of the actual code that you call. Rather, the documentation is telling you that the stage property is available on any instance of the DisplayObject class -- for example, a movieClip or a sprite.

For example, if you have a movieClip named foo, you could reference the stage with:

foo.stage

and go from there.

foo.stage.someRootLevelObject.htmlText = "Pretty <b>easy</b>";

Upvotes: 2

Related Questions