简体   繁体   中英

actionscript 3.0 XML list menu into for loop

I'm new to actionscript and want to write the following code in for loop style. It is a menu loaded from a xml file

nav_menu.nav_1.text=gallery_xml.gallery[0].@name;
nav_menu.nav_2.text=gallery_xml.gallery[1].@name;
nav_menu.nav_3.text=gallery_xml.gallery[2].@name;
nav_menu.nav_4.text=gallery_xml.gallery[3].@name;
nav_menu.nav_5.text=gallery_xml.gallery[4].@name;
nav_menu.nav_6.text=gallery_xml.gallery[5].@name;
nav_menu.nav_7.text=gallery_xml.gallery[6].@name;

I tried write as an array. The tracing result of the array was right. But I don't know how to put the array into the dynamic textbox...

Thank you very much for helping.

Use XMLList class firstly like belove.

var list:XMLList = gallery_xml.gallery;

After that while you are making it a loop, try to make menu items dynamically. You should adjust menu places according to need.

var nav_menu:Sprite = new Sprite();
addChild(nav_menu);
var navItem:Array = [];

var forX:Number = 0;
for(var i:int = 0; i < list.length(); i++){
   navItem[i] = new NavItem();
   navItem[i].tx_txt.autoSize = TextFieldAutoSize.LEFT;
   navItem[i].tx_txt.text = list[i].@name;
   navItem[i].x = forX;
   forX += navItem[i].width + 20; //(horizontal order with 20px space)
   nav_menu.addChild(navItem[i]);
}

You should have a NavItem with tx_txt field in the library, or a NavItem class (this is a hard way for beginners).

Note: I wrote the code in a webpage and it may have mistakes, but this is the way to go.

I think you're looking for a for loop Try something like this, assuming gallery is in fact an array.

for(var i:int = 0 ; i < gallery_xml.gallery.length ; i++){
     var menuText:int = i + 1; //since it looks like your nav_ text fields are not 0 indexed
     nav_menu['nav_' + menuText].text = gallery_xml.gallery[i].@name;
}

This will start at 0 in your gallery array and continue until 6 (your array length). The var i will represent the current index in the array. i + 1 looks like it's the corresponding text field

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM