View Full Version : dynamic onPress event
Amygdala
February 9th, 2003, 01:19 PM
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[i]);
sURL = "http://www.nothing.com";
set("MenuItem"+i+".onPress", "function () { getUrl("+sURL+"); }");
depth--;
yPos = yPos+30;
}
CyanBlue
February 9th, 2003, 08:47 PM
Howdy... :)
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[i];
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...
pom
February 10th, 2003, 08:53 AM
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[i]); 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...
pom
February 10th, 2003, 08:57 AM
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.
Amygdala
February 10th, 2003, 02:52 PM
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[i]);
set("MenuItem"+i+".sURL", arURL[i]);
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!
Amygdala
February 10th, 2003, 03:22 PM
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[i]);
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!
pom
February 10th, 2003, 06:16 PM
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:
Amygdala
February 11th, 2003, 02:29 AM
...that would also work. Sorry about the lack of code tags.
Thanx again for your help, though.
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.