Dynamic onPress event

Hi,

I’m trying to create a dynamic menu where the menu items are coming from an ASP page. I’m passing a string to the flash movie containing the menu headings, the flash script then creates the appropriate number of copies of a menuitem sub movie. This works fine. The problem however, is that I can’t seem to dynamically set the onPress event for each movie.

this is what i tried:

set(“MenuItem”+i+".onPress", “function () { getUrl(”+sURL+"); }");

It does not work. A hand appears on the movie when the mouse pointer hovers over it which leads me to believe the onPress event can be set this way, but nothing happens when I press the mouse button.

Any ideas how to set this event at runtime?

The full script looks like this:

MenuItem._visible = 0;
arTitles = titles.split(",");
menuLength = arTitles.length;
yPos = 20;
for (i=0; i<menuLength; i++) {
duplicateMovieClip(MenuItem, “MenuItem”+i, i);
setProperty(“MenuItem” add i, _y, yPos);
set(“MenuItem”+i+".item", arTitles*);
sURL = “http://www.nothing.com”;
set(“MenuItem”+i+".onPress", “function () { getUrl(”+sURL+"); }");
depth–;
yPos = yPos+30;
}

Howdy… :slight_smile:

Something like this???(Changed old format to FMX dot syntax)

MenuItem._visible = 0;
arTitles = titles.split(",");
menuLength = arTitles.length;
yPos = 20;
for (i = 0; i < menuLength; i++)
{
	duplicateMovieClip(MenuItem, "MenuItem" + i, i);
	this["MenuItem" + i]._y = yPos;
	this["MenuItem" + i].item = arTitles*;
	sURL = "http://www.nothing.com";
	this["MenuItem" + i].onPress = function ()
	{
		getURL(sURL);
	}
	depth--;
	yPos = yPos + 30;
}

Your for loop’s syntax is not right… See the difference…

I’m not sure this would happen here, but it’s very likely that it will:

Here you getURL(sURL);, but it’s probable that buttons will link to different URLs, so you’ll probably put them in an array and the use getURL(URL_array*); or something like this. The problem with this is that Flash doesn’t evalute i during the loop, but when you press the button. Which will mess up the whole thing. So careful with that…

I’m not sure I was really clear with that

URL_array=["www.thing.com","www.ting2.com"];
MenuItem._visible = 0;
arTitles = titles.split(",");
menuLength = arTitles.length;
yPos = 20;
for (i = 0; i < menuLength; i++)
{
	var mc=duplicateMovieClip(MenuItem, "MenuItem" + i, i);
	mc._y = yPos;
	mc.item = arTitles[i ];
	mc.onPress = function ()
	{
		getURL(URL_array[i ]);
	}
	depth--;
	yPos = yPos + 30;
}

will NOT work, because at the end of the loop, i equals menuLength so you’ll getURL(URL_array[menuLength]) which is outside the array.

You are both right.

CyanBlue: Your code does let me set the onPress event dynamically like I asked, but:

ilyaslamasse: You are absolutely right on all accounts. I did plan to use an array to populate the various onPress events, but as you pointed out: the variable is not used before it’s too late!!

My current solution is a rather dirty programmer’s trick :wink: . I store the url in an invisible textbox in the menuitem movie clip, and then let the function make a reference to that textbox’s variable instead.
Well, it’s not very elegant but it works. Any better solutions will be gratefully received! For example, can I create a global variable in the menuitem movie clip, without the use of an extra textbox?

here is the code:

MenuItem._visible = 0;
arTitles = titles.split(",");
arURL = urls.split(",");
menuLength = arTitles.length;
yPos = 20;
for (i=0; i<menuLength; i++) {
duplicateMovieClip(MenuItem, “MenuItem”+i, i);
setProperty(“MenuItem” add i, _y, yPos);
set(“MenuItem”+i+".item", arTitles*);
set(“MenuItem”+i+".sURL", arURL*);

this["MenuItem" + i].onPress = function ()
{
	getURL(this.sURL);
}

depth--;
yPos = yPos+30;

}

Actually, I don’t need to create the onPress function anymore, since it will always look the same (I just hard-coded it into the menuitem sub movie). It is only included here to make the code easier to understand.

thanx both of you, and please feel free to suggest improvements!

A little bit of research shows that the extra text box is not necessary. I experimentally removed the text box, and the links still work.

I guess this line:

set(“MenuItem”+i+".sURL", arURL*);

automatically creates a global variable in the movie if one does not already exist.

Brilliant! This ASP menu keeps getting better!
Except this prototype is so boring now it’s not really distinguishable from a pure HTML menu… Time to learn some of that fancy Flash magic!

I’m sorry, I can’t read your code that well, you forgot to use the code tag.
Anyway, the usual trick I use is to store i inside the clip. Like so

URL_array=["www.thing.com","www.ting2.com"];
MenuItem._visible = 0;
arTitles = titles.split(",");
menuLength = arTitles.length;
yPos = 20;
for (i = 0; i < menuLength; i++)
{
	var mc=duplicateMovieClip(MenuItem, "MenuItem" + i, i);
	mc._y = yPos;
	**mc.i=i;**
	mc.item = arTitles[i ];
	mc.onPress = function ()
	{
		getURL(URL_array[**this.i**]);
	}
	depth--;
	yPos = yPos + 30;
}

That way you get the right value for i.

pom :phil:

…that would also work. Sorry about the lack of code tags.
Thanx again for your help, though.