Skip to main content
Known Participant
February 17, 2010
Answered

Questions about AS2 Tree Component (Flash CS3)

  • February 17, 2010
  • 2 replies
  • 1346 views

I have Flash application with a Tree component on the Stage. I'm using an XML file to create the structure of the Tree component.

Each entry on my XML has the attributes id, label, heading, desc, and icon.

My problem is that I can't seem to be able to set the Tree to default with the first node open.

My code is:

var xmlTreeData:XML = new XML();
xmlTreeData.onLoad = function() {
mTree.dataProvider = this.firstChild;
};
xmlTreeData.ignoreWhite = true;
xmlTreeData.load("xml/treeValues1.xml");
mTree.setStyle("fontSize","11");
mTree.setStyle("selectionColor","0xE0E0E0");
mTree.setStyle("useRollOver",false);
mTree.setIsOpen(mTree.getTreeNodeAt(0), true);

My tree populates fine, and the handler I retrieve whatever item is clicked works great. However, the tree refuses to default to having that first node open.

I'm also interested in placing custom icons into certain nodes via the icon attribute I've set in the XML, but that will have to come later I guess.

This topic has been closed for replies.
Correct answer Rothrock

I've never used the Tree component, so I'm just guessing here....

There is a slight delay before the xml file loads and your tree has its data provider.

Perhaps during that time there is no node at mTree.getTreeNodeAt(0) to open? Try adding this just before the last line of your code:

trace("the node is: "+mTree.getTreeNodeAt(0));

What do you get?

You might need to move that code inside the xml's load event handler.

Another thing is that the Flash components tend to work on an invalidate-then-wait-one-frame-to-redraw kind of model. So it is possible you will need to wait one frame after the xml has loaded and then tell it to open that node.

The doLater method might help with that.

var home:MovieClip=this;

var xmlTreeData:XML = new XML();
xmlTreeData.onLoad = function() {
mTree.dataProvider = this.firstChild;

mTree.doLater(home,"delay");
};


xmlTreeData.ignoreWhite = true;
xmlTreeData.load("xml/treeValues1.xml");
mTree.setStyle("fontSize","11");
mTree.setStyle("selectionColor","0xE0E0E0");
mTree.setStyle("useRollOver",false);

function delay(){

2 replies

Participant
August 5, 2010

Hi, I have the same issue with my code and I haven´t been able to make it open the first node, could yo plase give any help?

Here´s my AS

Stage.addListener(this)

onResize()

var __this=this

function onResize(){

menu_tree._x = POSITION_X_MENU  //WIDTH_GALLERY/2-menu_tree._width/2;

menu_tree._y =  POSITION_Y_MENU ////HEIGHT_GALLERY/2-menu_tree._height/2;

}

/////////////////////////////////////////////////////////

menu_tree.mod_tre.iconFunction = function(item:Object):String  {

if (!item.hasChildNodes()) {

return "TreeFolderClosed";

}

};

////////////////////////////////////////////

function change_cat(obj:Object) {

Stage.removeListener(__this)

var tree = obj.target;

var sel_tn = menu_tree.mod_tre.selectedNode;

if (tree.getIsBranch(sel_tn)) {

var nodeAlreadyOpen_bool = tree.getIsOpen(sel_tn);

menu_tree.mod_tre.setIsOpen(sel_tn, !nodeAlreadyOpen_bool);

}

if (!menu_tree.mod_tre.selectedNode.hasChildNodes()) {

if (mc_thumb._alpha == 0) {

mc_thumb._x = menu_tree._x+menu_tree._width+10;

mc_thumb._y = menu_tree._y;

mc_thumb.tween('_alpha', 100, 1);

}

mc_thumb.ini(menu_tree.mod_tre.selectedNode.attributes.xml, menu_tree.mod_tre.selectedNode.attributes.label);

}

}

//////////////////////////////////////////format

menu_tree.mod_tre.addEventListener("change", change_cat);

menu_tree.mod_tre.setStyle("borderStyle", "none");

menu_tree.mod_tre.setStyle("backgroundColor",0xbe071c);

menu_tree.mod_tre.setStyle("color",0xFFFFFF);

menu_tree.mod_tre.setStyle("rollOverColor", 0xF7F7F7);

menu_tree.mod_tre.setStyle("selectionColor", 0xF7F7F7);

menu_tree.mod_tre.setStyle("textRollOverColor", 0x000000);

menu_tree.mod_tre.setStyle("textSelectedColor", 0x000000);

menu_tree.mod_tre.vScrollPolicy = 'on';

///////////////////////////////////////////////

Thanks in advance

RothrockCorrect answer
Inspiring
February 17, 2010

I've never used the Tree component, so I'm just guessing here....

There is a slight delay before the xml file loads and your tree has its data provider.

Perhaps during that time there is no node at mTree.getTreeNodeAt(0) to open? Try adding this just before the last line of your code:

trace("the node is: "+mTree.getTreeNodeAt(0));

What do you get?

You might need to move that code inside the xml's load event handler.

Another thing is that the Flash components tend to work on an invalidate-then-wait-one-frame-to-redraw kind of model. So it is possible you will need to wait one frame after the xml has loaded and then tell it to open that node.

The doLater method might help with that.

var home:MovieClip=this;

var xmlTreeData:XML = new XML();
xmlTreeData.onLoad = function() {
mTree.dataProvider = this.firstChild;

mTree.doLater(home,"delay");
};


xmlTreeData.ignoreWhite = true;
xmlTreeData.load("xml/treeValues1.xml");
mTree.setStyle("fontSize","11");
mTree.setStyle("selectionColor","0xE0E0E0");
mTree.setStyle("useRollOver",false);

function delay(){

ankhcommAuthor
Known Participant
February 18, 2010

Moving the mTree.setIsOpen(mTree.getTreeNodeAt(0),true); statement into its onLoad function worked just as you suggested. I feel pretty silly for being stumped so long on that, but heck, it's my first XML script so maybe I shouldn't be so hard on myself heh.

Many thanks!