flash as3 - understanding how to pass parsed XML data on to classes

I've traditionally all my as3 code in one included .as file, not as packages / class files, but I'm trying to make that transition. I'm a little confused about how to pass information back and forth among class files though. For example, what I'm trying to make is a sort of node diagram where clicking on a certain node will sprout out its child nodes, and clicking on a child node will sprout out its children, etc. I have a main class, Main.as, which I was going to make parse an XML document and store an array of all the relevant data for each node (things like a title, link, etc). Then I have a class file called Node.as which actually builds the node, adds it to the stage, and sets up its listeners and the motion tweens.

My question is - if all the information for the nodes is stored in Main.as, but the click handling is done in Node.as, how does Node.as know what attributes to assign to each added node (i.e., how does it access the array from Main.as to assign properties to the clips created)?

Here's a sample code structure:

Main.as:

package  
{   
    public class Main extends MovieClip  
    {  

        public function Tree()  
        {  
            initTree();
        }

        public function initTree(){
            parseXML();
            var firstNode:Node = new Node();
            addChild(firstNode);
        }
    }  
}

Node.as:

package  
{  
    public class Node extends MovieClip  
    {   
        public function Node()  
        {  
            drawNode();
        }

    private function drawNode(parentNode = null){
        //draw the node shape
        //add the shape to a movie clip
        //assign listeners to the movie clip
        //add movie clip to the stage
    }

    private function nodeAdded {
        //position the node
        //animate the node
    }

    private function nodeOver (e:MouseEvent):void {
        //fetch the information for this node from the XML in Main.as - how do I do this?
    }
}

Answers


Pass to your node class instances the content they should display using parameteres in the constructor.

For example:

 package   {  
     public class Node extends MovieClip   {   

         private var contentToDisplay:XML;

         public function Node(_contentToDisplay:XML) {
              contentToDisplay = _contentToDisplay;
              drawNode();
         }

 }

In this way, you pass info when building instances of that class and store it as variable you can use internally in that instance. You can also set some "id" variable to help identify your node from outside the class. Or you create the nodes in your main class, pass them the info they should display and store a reference of the nodes created in an array in your main class.

Hope it helps!


Need Your Help

Which PHP opcode cacher should I use to improve performance?

php performance caching

I'm trying to improve performance under high load and would like to implement opcode caching. Which of the following should I use?

Asp.net MVC image caching in browser

asp.net-mvc-4 c#-4.0 caching browser-cache

In my asp.net/mvc (html 5) application, I have an "Add/Edit Product" wizard UI, which has 4 steps.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.