views:

2202

answers:

2

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?

+2  A: 

"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>";
bigmattyh
okay, but how do I use a string instead of hardcoding the objectname?
Stein G. Strindhaug
var objectName = 'someString';var object = foo.stage[objectName];
bigmattyh
+2  A: 

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?

Iain
I want to use string values to search for the name using a string (more often than i want to address a textarea), probably better ways to do it, though.
Stein G. Strindhaug
I tried your idea, it works fine when I just added `.name` in the search function.
Stein G. Strindhaug
The getChildByName function is not for adding assets from the library, it's for accessing children of a DisplayObjectContainer by name. (You may be thinking of flash.utils.getDefinitionByName())
aaaidan
You're right - why did I write that?! will edit.
Iain