Forums

Full Version: tree node path
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello,

this is a really simple one but sometimes the simple ones are the one needed by people around. The following methods returns the path form a tree node to the root node (via parent connection)

Code:
/**
   * Returns an array of all nodes starting from the given node to the tree root node
   * traversing along the successive parent node connection
   * @param n the node to start with
   * @return
   */
        
        private function _getTreeNodePathToRoot(n:NodeSprite):Array {
            
            var path:Array = new Array();
            path.push(n);            
            while (n.parentNode != null) {
                n = n.parentNode;
                path.push(n);
            }
            return path;
            
        }

and the following returns its reverse, the path from the root to the specified node

Code:
/**
   * Returns an array of all nodes starting from the tree root node to the given node
   * traversing along the respective child node connection
   * @param n the node to end up with
   * @return
   */
        
        private function _getTreeNodePathFromRoot(n:NodeSprite):Array {
            return _getTreeNodePathToRoot(n).reverse();
        }

cheers

martin
Reference URL's