all groups > flash actionscript > january 2004 >
You're in the

flash actionscript

group:

Problem with dynamically generated and named functions


Problem with dynamically generated and named functions Razorwyre
1/21/2004 9:15:43 PM
flash actionscript:
I'm trying to create a dynamically draw list of names that have rollovers and onmousedowns (like a datagrid list type thing), but done in my style (basically a bunch of movies with textfields inside of them that are both the size of the names to display). I'm trying to also on the fly create the onrollovers and the onmousedowns by passing a number to a function that should highlight the text that the mouse is currently over.

so:
if you mouse over the text inside Movie1 then I want the Movie1.onRollOver to call the function
highlight with the argument of 1...

The Problem: what happens is that all of the onrollover functions turn out in the end to have been passed the same value of i below (the largest value that the loop counter can be - num). Any ideas that could fix this code below and make it work properly?

Movie1:
onRollover = function()
{highlight(1);
}

....

Movie6:
onRollover = function()
{highlight(6);
}






for(var i=0;i<num;i++)
{
var moviename = "Movie"+i;
trace("I = " +i);
trace("MOVIE = "+moviename);
draw_mc[moviename].onRollOver = function()
{
highlight(i);
}
draw_mc[moviename].onMouseDown = function()
{
do_something(i);
}
}

Re: Problem with dynamically generated and named functions stwingy
1/21/2004 9:40:54 PM
something like this might work

num = 4;
function highlight(myvar) {
//just put this in because i don`t know exactly what you are doing but you can see how to reference
draw_mc["movie"+myvar]._visible = false;
}
for (var i = 0; i<num; i++) {
draw_mc["movie"+i].ivar = i;
draw_mc["movie"+i].onRollOver = function() {
highlight(this.ivar);
};
}

Certified but not by Macromedia!
AddThis Social Bookmark Button