# Dynamic onPress event

**URL:** <https://forum.kirupa.com/t/dynamic-onpress-event/13150>\
**Category:** Uncategorized\
**Created:** [February 9, 2003, 6:19pm UTC](https://forum.kirupa.com/t/dynamic-onpress-event/13150 "2003-02-09T18:19:46Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![Amygdala](https://avatars.discourse-cdn.com/v4/letter/a/e79b87/32.png) [@Amygdala](https://forum.kirupa.com/u/Amygdala)\
**Post date:** [February 9, 2003, 6:19pm UTC](https://forum.kirupa.com/t/dynamic-onpress-event/13150/1 "2003-02-09T18:19:46Z")

</div>

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](http://www.nothing.com)”;  
set(“MenuItem”+i+".onPress", “function () { getUrl(”+sURL+"); }");  
depth–;  
yPos = yPos+30;  
}

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 10, 2003, 1:47am UTC](https://forum.kirupa.com/t/dynamic-onpress-event/13150/2 "2003-02-10T01:47:29Z")

</div>

Howdy… 🙂

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

```auto
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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 10, 2003, 1:53pm UTC](https://forum.kirupa.com/t/dynamic-onpress-event/13150/3 "2003-02-10T13:53:50Z")

</div>

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…

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 10, 2003, 1:57pm UTC](https://forum.kirupa.com/t/dynamic-onpress-event/13150/4 "2003-02-10T13:57:20Z")

</div>

I’m not sure I was really clear with that

```auto
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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 10, 2003, 7:52pm UTC](https://forum.kirupa.com/t/dynamic-onpress-event/13150/5 "2003-02-10T19:52:39Z")

</div>

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 😉 . 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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 10, 2003, 8:22pm UTC](https://forum.kirupa.com/t/dynamic-onpress-event/13150/6 "2003-02-10T20:22:27Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 10, 2003, 11:16pm UTC](https://forum.kirupa.com/t/dynamic-onpress-event/13150/7 "2003-02-10T23:16:19Z")

</div>

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

```auto
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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [February 11, 2003, 7:29am UTC](https://forum.kirupa.com/t/dynamic-onpress-event/13150/8 "2003-02-11T07:29:06Z")

</div>

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