Flash CS3 - Flash CS4

Free tutorials and scripts for all.
Actionscript 3.0

XML menu tree

This is a discussion on XML menu tree within the advanced Actionscript 3.0 forums, part of the Flash CS3 eng category; Hi Flep! first of all, i would like to congratulate you for this site, its a perfect resource place for ...


Go Back   Forum Flash CS3 Flash CS4 > English Forums > Flash CS3 eng > advanced Actionscript 3.0

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  2 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 09-12-07, 16:17
Junior Member
 
Join Date: Aug 2007
Location: Brasil
Posts: 18
Rep Power: 0
Magdy is on a distinguished road
Question XML menu tree

Hi Flep!

first of all, i would like to congratulate you for this site, its a perfect resource place for flash lovers! Well done!!

Now my doubts!
Hope you can help me...

Iīm developing a web site and iīm trying to make a menu tree that receive info from xml file. Well, I already built the menu (without effects, simple) the flash receives the xml info but what I really want is something like this: Flash Accordion Tree Menu V3 - Flash Component

Below itīs the code for the .fla file:
Code:
import fl.transitions.Tween;
import fl.transitions.easing.*;

var menuText:TextField;
var menuSubText:TextField = new TextField();

var myTween:Tween;

var xml:XML;
var xmlList:XMLList;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("xml/clientesBrasil.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event):void
{
    xml = XML(event.target.data);
    xmlList = xml.children();
    for (var i:int = 0; i < xmlList.length(); i++)
    {
        menuText = new TextField();
        menuText.text = xmlList[i].attribute("TIPO");
        menuText.y = i * 20;
        menuText.autoSize = TextFieldAutoSize.LEFT;
        menuText.name = xmlList[i].attribute("TIPO");
        addChild(menuText);
        menuText.addEventListener(MouseEvent.CLICK, onClick);
    }
}

function onClick(event:MouseEvent):void
{
    menuSubText.x = 200;
    menuSubText.y = j * menuText.height;
    menuSubText.autoSize = TextFieldAutoSize.LEFT;
    addChild(menuSubText);
    for (var j:int = 0; j < xmlList.length(); j++)
    {
        if (xmlList[j].attribute("TIPO") == event.target.name)
        {
            menuSubText.text = xmlList[j].children();
            myTween = new Tween(menuText, "y", Strong.easeOut, menuText.y, menuSubText.height, .25, true);
        }
    }
}
and here is the info from xml file:
Code:
<CB>
    <TC TIPO="MENU1">
        <CT>SUBMENU1</CT>
        <CT>SUBMENU2</CT>
        <CT>SUBMENU3</CT>
        <CT>SUBMENU4</CT>
        <CT>SUBMENU5</CT>
        <CT>SUBMENU6</CT>
    </TC>
    <TC TIPO="MENU2">
        <CT>SUBMENU1</CT>
        <CT>SUBMENU2</CT>
        <CT>SUBMENU3</CT>
        <CT>SUBMENU4</CT>
        <CT>SUBMENU5</CT>
    </TC>
    <TC TIPO="MENU3">
        <CT>SUBMENU1</CT>
        <CT>SUBMENU2</CT>
        <CT>SUBMENU3</CT>
        <CT>SUBMENU4</CT>
        <CT>SUBMENU5</CT>
        <CT>SUBMENU6</CT>
        <CT>SUBMENU7</CT>
    </TC>
    <TC TIPO="MENU4">
        <CT>SUBMENU1</CT>
        <CT>SUBMENU2</CT>
    </TC>
</CB>
With those codes, I could do a menu but not exactly like what I want.
One more thing, if you test this code, you will notice that the sub menu items appear like this <CT>SUBMENU1</CT> with the <CT></CT>. How can I take this out from not appearing?

Well this is my problem...
Really hope you can help me.
Thanks for all your dedication throw your site and for the attention!!

Magdy
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #2 (permalink)  
Old 10-12-07, 07:59
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Re: XML menu tree

Hi

I don't like to use XMLList class.
I would do:
Code:
var xml:XML=new XML();
var xmlList:XMLList;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("test.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event):void
{
    var xml:XML=new XML(xmlLoader.data);
	var myXML:XMLDocument=new XMLDocument();
	myXML.ignoreWhite=true;
	myXML.parseXML(xml.toXMLString());
	var node:XMLNode=myXML.firstChild;
	
    for (var i:int = 0; i < node.childNodes.length; i++)
    {
        var j:int=(node.childNodes[i].childNodes.length);
		for(var k:int=0;k<j;k++)
		{
			trace(node.childNodes[i].childNodes[k].firstChild.nodeValue);
		}
    }
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #3 (permalink)  
Old 11-12-07, 17:49
Junior Member
 
Join Date: Aug 2007
Location: Brasil
Posts: 18
Rep Power: 0
Magdy is on a distinguished road
Re: XML menu tree

Hi Flep!

Thank you for the help, but why you donīt like xmlList class?
and with the code you post, I would like to add a button function but Iīm getting some errors.

below is the code: (if its possible, tell me what itīs wrong)
Code:
import fl.transitions.Tween;
import fl.transitions.easing.*;

var menuText:TextField;
var menuSubText:TextField = new TextField();

var myTween:Tween;

var xml:XML=new XML();
var xmlList:XMLList;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("xml/clientesBrasil.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event):void
{
    var xml:XML = new XML(xmlLoader.data);
    var myXML:XMLDocument=new XMLDocument();
    myXML.ignoreWhite = true;
    myXML.parseXML(xml.toXMLString());
    var node:XMLNode = myXML.firstChild;
    for (var i:int = 0; i < node.childNodes.length; i++)
    {
        menuText = new TextField();
        menuText.y = i * 20;
        menuText.autoSize = TextFieldAutoSize.LEFT;
        menuText.text = node.childNodes[i].attributes.TIPO;
        addChild(menuText);

        var j:int = (node.childNodes[i].childNodes.length);
        for (var k:int = 0; k < j; k++)
        {
            menuText.addEventListener(MouseEvent.CLICK, onClick);
            function onClick(event:MouseEvent):void
            {
                trace(node.childNodes[i].childNodes[k].firstChild.nodeValue);
            }
        }
    }
}
Thanks for the attention!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #4 (permalink)  
Old 12-12-07, 07:05
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Re: XML menu tree

Hi,
I don't know the why but ... i feel better using this technic.

What's the errors you get ?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #5 (permalink)  
Old 12-12-07, 13:31
Junior Member
 
Join Date: Aug 2007
Location: Brasil
Posts: 18
Rep Power: 0
Magdy is on a distinguished road
Re: XML menu tree

Hi Flep!

Fist of all I will post the code Iīm using:
Code:
var menuText:TextField;
var menuSubText:TextField = new TextField();


var xml:XML = new XML();
var xmlList:XMLList;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("xml/clientesBrasil.xml"));
xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event):void
{
    var xml:XML = new XML(xmlLoader.data);
    var myXML:XMLDocument=new XMLDocument();
    myXML.ignoreWhite = true;
    myXML.parseXML(xml.toXMLString());
    var node:XMLNode=myXML.firstChild;

    for (var i:int = 0; i < node.childNodes.length; i++)
    {
        menuText = new TextField();
        menuText.y = i * 20;
        menuText.autoSize = TextFieldAutoSize.LEFT;
        menuText.selectable = false;
        menuText.text = node.childNodes[i].attributes.TIPO;
        addChild(menuText);
        menuText.addEventListener(MouseEvent.CLICK, onClick);

        var j:int = (node.childNodes[i].childNodes.length);
        function onClick(event:MouseEvent):void
        {
            for (var k:int = 0; k < j; k++)
            {
                menuSubText.x = 200;
                menuSubText.y = k * 20;
                menuSubText.autoSize = TextFieldAutoSize.LEFT;
                addChild(menuSubText);

                menuSubText.text = node.childNodes[i].childNodes[k].firstChild.nodeValue;
                //trace(node.childNodes[i].childNodes[k].firstChild.nodeValue);
            }
        }
    }
}
Well I get an error of undefined property when I add a button function inside the for loop and call the subitems using this code:
Code:
menuSubText.text = node.childNodes[i].childNodes[k].firstChild.nodeValue;
I resolve this by adding the for loop to the button function and substituting the i for j on the code above, but I still have a problem, when I click on the first item it was supposed to show the subitems of the first item and the same for the rest. what I get is only one item. I have no idea of what Iīm doing wrong.

If its possible, test this code to see the error.
I really need help on this...

Thanks a lot!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Flash Multi Gallery
  #6 (permalink)  
Old 12-12-07, 13:41
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Re: XML menu tree

You can't use the onClick function in that way.

For example, when you click an item, the firt loop ( i ) is no more active, so the function does a loop only ( k ).

First of all, you should load the xml and push all the values into Arrays.
After that, you add the MouseEvent and onClick function and inside it you get the values you need from Arrays.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #7 (permalink)  
Old 12-12-07, 15:38
Junior Member
 
Join Date: Aug 2007
Location: Brasil
Posts: 18
Rep Power: 0
Magdy is on a distinguished road
Re: XML menu tree

Iīm confused on how to push the values into array.
I read the tutorial about arrays form your site, and try this:

Code:
var xmlArrays:Arrays = new Array()

xmlArrays.push(node.childNodes[i].childNodes[k].firstChild.nodeValue);
trace(xmlArrays);
but where to add the push function? Inside the onClick function? I will need another for loop to show the subitens?

Hey Flep sorry if Iīm disturbing you, but you are the only one from the foruns I post that tries to aswer my question.

So, I'm really very glad for your attention
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #8 (permalink)  
Old 12-12-07, 21:35
Administrator
 
Join Date: Jul 2007
Location: Cesena
Posts: 4,486
Rep Power: 6
Flep is on a distinguished road
Re: XML menu tree

You should push the values. ( not inside the onClick function ).
And, you can use a bidimensional Array like this sample:

Multiple Gallery XML Slideshow
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #9 (permalink)  
Old 13-12-07, 08:57
hard_overclocker's Avatar
Moderator
 
Join Date: Jul 2007
Posts: 51
Rep Power: 2
hard_overclocker is on a distinguished road
Re: XML menu tree

Hey Magdy, here is one XML menu I created for a website. Take a look at the source and perhaps you can get some help out of it. The problem is that the code is bloated with all kind of junk workarounds (my type of coding i'm afraid).
Hope this helps, if not perhaps we will have a tutorial here soon about this.

I have uploaded the code.

Happy flashing. Ciao
Attached Files
File Type: rar myTreeMenu.rar (40.5 KB, 74 views)


Last edited by Flep; 13-05-08 at 20:05..
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

  #10 (permalink)  
Old 13-12-07, 20:42
Junior Member
 
Join Date: Aug 2007
Location: Brasil
Posts: 18
Rep Power: 0
Magdy is on a distinguished road
Re: XML menu tree

Hi hard_overclocker!!

Thank you very much for your menu tree source, I understand some things, but Iīm still confused with others... :(
Hope we could have a tutorial on how to do this xml menu tree soon here. :)
It will be fantastic!!

Well first o all Iīm not repeating my first post. I just want to understand somethings so I will post my original code using xmlList (sorry Flep! :P - this code works perfect with your code too!) and would like a help on how to add a tween effect when the Top level item was clicked. For example: if top level 1 is clicked, all the others top levels "y" property will change (with tween effect) according to the length of the subitems of the top level 1. And so on for the others top levels.

Code:
import fl.transitions.Tween;
import fl.transitions.easing.*;

var menuText:TextField;
var menuSubText:TextField = new TextField;
var myTween:Tween;

var xml:XML;
var xmlList:XMLList;
var xmlLoader:URLLoader = new URLLoader();
xmlLoader.load(new URLRequest("xml/clientesBrasil.xml"));

xmlLoader.addEventListener(Event.COMPLETE, xmlLoaded);

function xmlLoaded(event:Event):void
{
    xml = XML(event.target.data);
    xmlList = xml.children();
    for (var i:int = 0; i < xmlList.length(); i++)
    {
        menuText = new TextField();
        menuText.y = i * 20;
        menuText.text = xmlList[i].attribute("TIPO");
        menuText.name = xmlList[i].attribute("TIPO");
        menuText.autoSize = TextFieldAutoSize.LEFT;
        addChild(menuText);
        menuText.addEventListener(MouseEvent.CLICK, onClick);
    }
}

function onClick(event:MouseEvent):void
{
    for (var j:int = 0; j < xmlList.length(); j++)
    {
        menuSubText.x = 200;
        menuSubText.autoSize = TextFieldAutoSize.LEFT;
        addChild(menuSubText);
        if (xmlList[j].attribute("TIPO") == event.target.name)
        {
            menuSubText.text = xmlList[j].children();
            myTween = new Tween (menuText, "y", Strong.easeOut, menuText.y, menuSubText.height, 1, true);
        }
    }
}
If you could test this code using the xml file I post in the first post, you will see that the "y" tween effect itīs woking, but only the last top level change the "y" property (MENU 4). Itīs really necessary to use arrays to apply this tween effect to the others top levels items?

Iīm asking this because if you remove the tween effect, and test the movie, you will see that the menu works fine, top level clicked shows the correct subitem.

Iīm not an expert on AS3, but i imagine itīs kind simple to do this, but itīs not working...:(

Hope I explanated what is confusing me.
Again, I would like to thank for all your help!!

By!!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads

Thread Thread Starter Forum Replies Last Post
Load directory structure in tree component fisad Flash CS3 eng 0 17-04-08 23:08
menu tree ad albero in su sk8benji Flash CS3 generale 2 01-04-08 13:45
[propagazione di eventi] menu tree pepigno75 Actionscript 3.0 avanzato 2 24-03-08 14:42


All times are GMT. The time now is 22:39.


Powered by vBulletin versione 3.7.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC4
Forum SiteMap


FlepStudio
by Filippo Lughi
P.IVA 03605860406