Get all named instances in a flash movie

Is it possible to get a list of all named instances in a flash movie using actionscript 2. Like an array containing all named textareas or all instances of movieClip, etc., I would like to get this so the designer can add or remove textareas and so on, and ActionScript can find them dynamically and get texts from a separate data source.

I'm guessing I want something like a DOM tree or even better something like how getElementByName () works in JavaScript. And also get the string value of the instance name so that I can find its text value in the xml.

Let's say the designer adds a new text area called "copyright" and my code should (without having to change the script) find the data with the id "copyright" in the XML file, if any, and add the value to the text area.

0


source to share


3 answers


@Stein Gauslaa Strindhaug

It's pretty crude, but it should do the trick!

private function traceAllChildren(rootContainer:DisplayObjectContainer):void {
    for(var i=0; i < rootContainer.numChildren; i++) {
        var item:* = rootContainer.getChildAt(i);
        try {
            traceAllChildren(item);
        } catch (e:Error) {
            trace(e.toString());
        }
        trace(item.toString());
        // This is the block where you can affect
        // an 'item' depending on it type, name, etc..
        // eg: 
        //      if (item.toString() == '[object TextField]') {
        //          item.text = "The text I want to insert";
        //      }
        // or
        //      if (item.name == myTextFieldNameVar) {
        //          item.text = "The text I want to insert";
        //      }
    }
}

      



Then call it like traceAllChildren (this) or traceAllChildren (myParentMovieClip).

Hope this helps, good luck!

+2


source


Short answer: there is no "built-in" way to do this. You could try to tweak functionality similar to getElementByName (), but it would require starting at _root

and scrolling through your content - and, strictly speaking, a video clip starts with links to its children, but these can be removed at runtime, so this might fail.



With that said, this is a common problem and there are many approaches to it. One thing you might find more useful is to create a custom component attached to a custom class - say "TextPlaceholder" and have your designer place copies of where they want dynamic text. Then, at runtime, this component can examine its property _name

or other settings of the custom component, and based on this, it can create a text box of appropriate size, content, etc. This is just one way to get closer to him.

+1


source


It's been a while since I did any AS2 coding, but perhaps you could use a combination of MovieClip.getInstanceAtDepth () and this.getNextHighestDepth () to find the deepest depth in your movie and then trace each bottom depth until you find getInstanceAtDepth () that matches the clip you want to populate with new data.

I also noticed this sample code in the AS2 documentation.

The following code tracks the depth of all video instances in the Stage:

for (var i in this) {
    if (typeof (this[i]) == "movieclip") {
    trace("movie clip '"+this[i]._name+"' is at depth "+this[i].getDepth());
    }
}

      

PS: I know you probably don't want to hear this, but in AS3 it's a breeze as you can just iterate over it. children!

0


source







All Articles