PDA

View Full Version : Duplicate movie clip for dynamic menu


zipkundan
10-27-2009, 08:39 AM
Dear Friends,
I am trying to create a dynamic vertical menu with varying sub menu items in each main menu item. I am loading external xml file to get the menu items text. I stored data from xml in an Array wtih "for" loop.

Also I tried to duplicate a pre created movie clip. (movie clip name is "menuItem". Already placed on stage.) This was done fine.

All Ok so far. Now I am trying to combine these 2 things. But the "duplicateMovieClip" doesn't work.

Following is the code I am trying. Please advice what is wrong or what has to be done to achieve the goal.

//Following to load XML
var menuXML:XML = new XML();
menuXML.load("sample_menu_list_1.xml");
menuXML.ignoreWhite = true;

menuXML.onLoad = function(success) {
if (success) {
/* =================== */
var menuArray = new Array();
for (i=0; i<menuXML.firstChild.childNodes.length; i++) {
menuArray.push(menuXML.firstChild.childNodes[i].firstChild.nodeValue);
trace(menuArray.length);
//following for duplicating movie clip
var mcName:String = "menuItem"+(i+1);
trace("MC name is "+mcName);
var mcDepth:Number = i+1;
trace("MC depth is "+mcDepth);
var newMC:MovieClip = _root.duplicateMovieClip(mcName, mcDepth);
trace(newMC);
//duplicating movie clip ends
}
}
};
//For XML ends

I am very with actionscript. Any help is deeply appreciated.
Thanks.

zipkundan
10-27-2009, 09:07 AM
Dear fiends,

I have found a way around. I am posting the code that I have updated. i don't know it will be useful or not. But thought to put it anyway.

//Following to load XML
var menuXML:XML = new XML();
menuXML.load("sample_menu_list_1.xml");
menuXML.ignoreWhite = true;

menuXML.onLoad = function(success) {
if (success) {
/* =================== */
var menuArray = new Array();
for (i=0; i<menuXML.firstChild.childNodes.length; i++) {
menuArray.push(menuXML.firstChild.childNodes[i].firstChild.nodeValue);
trace(menuArray.length);
//following for duplicating movie clip
var mcName:String = "menuItem"+(i+1);
trace("MC name is "+mcName);
var mcDepth:Number = i+1;
trace("MC depth is "+mcDepth);
var newMC:MovieClip = _root.menuItem.duplicateMovieClip(mcName, mcDepth);
trace(newMC);
newMC.menuText.text = menuArray[i];
newMC._x = 10;
var mcY:Number = 30 + (newMC._height * i);
trace("MC Y is "+mcY);
newMC._y = mcY;
//duplicating movie clip ends
}
}
};
//For XML ends

I am not sure if this is the right way to do it, but atleast it served my purpose. Any suggestion or advice for improvement is most welcome.