Delay (setInterval) with attachmovie

i created a menu using the xml tute example and made some modifications to what i needed but now need to add a delay when using attachmovie to add clips to the stage. I have tried to use setInterval, but couldnt get it to work so far.

This is part of the code im using to attach the movies to the stage…

// for all items or XML nodes (items and menus)
// within this node_xml passed for this menu
for (var i=0; i<node_xml.childNodes.length; i++) {

// movieclip for each menu item
if (node_xml.childNodes*.nodeName == "menu"){
curr_item = curr_menu.attachMovie("menuitem","item"+i+"_mc", i);
}else{
curr_item = curr_menu.attachMovie("menuitem1","item"+i+"_mc", i);
};

I need the for loop where it is as it assigns all the values to each clip down the bottom of the page.

At the moment im using the if statement to assign different clips depending on whether the item is a menu or a submenu item.

Any ideas how i would add a delay inbetween each of the attachmovies?

Here’s the whole as if it makes sense… xmlmenu.fla.as

How about something like this:

// for all items or XML nodes (items and menus)
// within this node_xml passed for this menu
attach_counter = 0;

// Now set the delay for 1 second
delay = setInterval(attachCallback, 1000);

function attachCallback()
{
// movieclip for each menu item
if (node_xml.childNodes[attach_counter].nodeName == “menu”){
curr_item = curr_menu.attachMovie(“menuitem”,“item”+attach_counter+"_mc", attach_counter);
}else{
curr_item = curr_menu.attachMovie(“menuitem1”,“item”+attach_counter+"_mc", attach_counter);
}

if (++attach_counter >= node_xml.childNodes.length)
{
clearInterval(delay);
}
}

thanks for the reply, tried it but it didnt seem to work though.

the preloader i have on the file just kept looping back to the start when i added in that code.

This is the fla file im using at the moment with most of the elements stripped out…

[URL=“http://www.agimedia.net/temp_images/flash.zip”]
flash.zip

Ok it looks like Preloader.as wasn’t included in the zip.

oops sorry about that, just updated the file with preloader as in it.

flash.zip

Well I can’t upload the fla because it’s too big, but here are the changes I made to the code… I added the AttachMenus function to handle callbacks from the timer.

// generates a list of menu items (effectively one menu)
// given the inputted parameters. This makes the main menu
// as well as any of the submenus
GenerateMenu = function(container, name, x, y, depth, node_xml) {
// variable declarations
var curr_menu = container.createEmptyMovieClip(name, depth);

// for all items or XML nodes (items and menus)
// within this node_xml passed for this menu
attachQ = new Array();
for (var i=0; i<node_xml.childNodes.length; i++) {
// movieclip for each menu item
var mc = “menuitem”;
if (node_xml.childNodes*.nodeName != “menu”){
mc = “menuitem1”;
}
attachQ.push({idx: i, mcname: mc, node: node_xml.childNodes*, x: x, y: y});
}

delay = setInterval(AttachMenus, 1000, curr_menu);
}
AttachMenus = function(curr_menu)
{
var curr_node;
var curr_item;
if (attachQ.length == 0)
{
clearInterval(delay);
return;
}
var info = attachQ.shift();

curr_item = curr_menu.attachMovie(info.mcname,“item”+info.idx+"_mc", info.idx);

curr_item._x = info.x;
curr_item._y = info.y + info.idx*curr_item._height;

curr_item.background._alpha = 0;
tTasks._alpha = 40;
tRoles._alpha = 40;

// item properties assigned from XML
curr_node = info.node;
curr_item.action = curr_node.attributes.url;
curr_item.roles = curr_node.attributes.roles;
curr_item.tasks = curr_node.attributes.tasks;
curr_item.name.text = curr_node.attributes.title;

// item submenu behavior for rollover event
if (curr_node.nodeName == “menu”){
// open a submenu
curr_item.node_xml = curr_node;
curr_item.onRollOver = curr_item.onDragOver = function(){
tweenMenu(slider._y,this._y);
GenerateMenu(curr_menu, “submenu_mc”, 315, 60, 1000, this.node_xml);
};

}else{ // nodeName == “item”

// close existing submenu
curr_item.onRollOver = curr_item.onDragOver = function(){
tasks.text = this.tasks
roles.text = this.roles
curr_menu.submenu_mc.removeMovieClip();

// show a hover color and show titles at full colour
var menuTween:Tween = new Tween(this.background, “_alpha”, Strong.easeOut, 0, 30, .5, true);
var menuTween:Tween = new Tween(_root.tTasks, “_alpha”, Strong.easeOut, 40, 100, .5, true);
var menuTween:Tween = new Tween(_root.tRoles, “_alpha”, Strong.easeOut, 40, 100, .5, true);
};

curr_item.onRollOut = curr_item.onDragOut = function(){
tasks.text = “”;
roles.text = “”;
// restore color
var menuTween:Tween = new Tween(this.background, “_alpha”, Strong.easeOut, 30, 0, .9, true);
var menuTween:Tween = new Tween(_root.tTasks, “_alpha”, Strong.easeOut, 100, 40, .5, true);
var menuTween:Tween = new Tween(_root.tRoles, “_alpha”, Strong.easeOut, 100, 40, .5, true);
};

// any item, menu opening or not can have actions
curr_item.onRelease = function(){
getURL(this.action);
};

}
};

Thanks so much for this it seems to be working like a charm…

Only one thing i have noticed with this is that, if the mouse is over one of the left hand buttons when its loading the menu item seem to stop up to that certain point. i.e if the mouse is accidently at the same point as where menu item 3 is nothing will show underneath it. Any ideas as to why that happens…

Again thank you so much for your help…

Just figured out that is caused by the second level menu generation function on the rollOver event

GenerateMenu(curr_menu, “submenu_mc”, 315, 60, 1000, this.node_xml);

edit - IT ALL WORKS NOW.

Applied the generatemenu function when attachQ = 0, and its all sweet.

Thanks again