-
For Loops using i
Hi Guys,
I have 20 buttons that I want to define actions for. Instead of defining 20 separate call back functions, I wanted to do this in a FOR loop since all of the buttons share the same action. My buttons are named "btn1_mc" through "btn20_mc". So, I created this for loop:
------BEGIN
for (i=1; i<20; i++) {
buttons_mc.btn[i+_mc].onRollOver = function() {
myColor = new Color(this);
myColor.setRGB(0xFFFF00);
_root.name_txt.text = this.btn+[i+_txt].text;
};
buttons_mc.btn[i+_mc].onRollOut = function() {
myColor.setRGB(0xFFFFFF);
_root.name_txt.text = "";
};
}
------END
But this doesn't work. I traced "i" and it is incremeting properly, but for some reason my buttons aren't responding...any idea what I'm doing wrong?? Do i need to store the button names in an array first?
Thanks,
Josiah
-
Hi,
Could the extra '+' before [i+_txt] in this line be causing the problem? :
root.name_txt.text = this.btn+[i+_txt].text;
Just looks odd to me. (Which might show how little I know )
-
Yeah that doesn't matter...it's actually supposed to go in the bracket, either way will work. But i'm not concerned as much about that as I am getting the rollovers to work on the btn[+i+_mc] instances.
-
Try:
buttons_mc["btn"+i+"_mc"].onRollOver = function() {
and
_root.name_txt.text = this["btn"+i+"_txt"].text;
K.
-
Deadbeat,
You're the man...that was it!! Thanks so much man. So I guess I had an extra dot in there that i didn't need? Thanks man, you were a HUGE help.
Best,
Josiah
-
Actually, it was the lack of quotes that was giving you problems ("_mc" instead of _mc)...text within quotes is evaluated as a literal string, without the quotes it attempts to evaluate it as a variable (which in this case didn't exist)...
Cheers,
K.
-
Oh okay, thanks.
but why no dot after "buttons_mc" and "this"??
-
Don't need the dot when using array access...
K.
-
One last question for you...calling "i" again within the first function doesn't work and results in undefined. How do i call to the text field inside the "btn"+i+"_mc" within the rollover function?
-
Do you mean this?
buttons_mc["btn"+i+"_mc"].onRollOver = function() {
myColor = new Color(this);
myColor.setRGB(0xFFFF00);
_root.name_txt.text = this["btn"+i+"_txt"].text;
};
K.
-
Yes, for some reason it is saying UNDEFINED, and the text rollover is only working for the last button instance (btn20_mc).
-
I found a work around, where I renamed all the text field instances in the button movie clips to all be the same name...and it works. But is there another way to do it?
Here is what i changed it to:
_root.name_txt.text = this.btn_txt.text;
-
Having problems calling "i" withinthe onRollOver function, the last line of code...it only remembers "items20"
PHP Code:
for (i=1; i<21; i++) {
//myText = "btn"+i;
buttons_mc["btn"+i+"_mc"].onRollOver = function() {
////change text field to yellow on rollovers
myColor = new Color(this);
myColor.setRGB(0xFFFF00);
//Display Name in larger text
_root.name_txt.text = this.btn_txt.text;
//Hide description text and family kit picture
_root.see_product_mc._visible = false;
_root.description_txt._visible = false;
//show detailed product
//THIS IS THE PART THAT DOESNT WORK
items_mc.gotoAndStop("items"+i);
}
buttons_mc["btn"+i+"_mc"].onRollOut = function() {
myColor.setRGB(0xFFFFFF);
_root.name_txt.text = "";
_root.see_product_mc._visible = true;
_root.description_txt._visible = true;
};
}
-
Try this:
code:
for (i=1; i<21; i++) {
buttons_mc["btn"+i+"_mc"].itemNum=i;
buttons_mc["btn"+i+"_mc"].onRollOver = function() {
////change text field to yellow on rollovers
myColor = new Color(this);
myColor.setRGB(0xFFFF00);
//Display Name in larger text
_root.name_txt.text = this.btn_txt.text;
//Hide description text and family kit picture
_root.see_product_mc._visible = false;
_root.description_txt._visible = false;
//show detailed product
items_mc.gotoAndStop(this.itemNum);
}
buttons_mc["btn"+i+"_mc"].onRollOut = function() {
myColor.setRGB(0xFFFFFF);
_root.name_txt.text = "";
_root.see_product_mc._visible = true;
_root.description_txt._visible = true;
};
}
HTH,
K.
-
Works like a charm...you ROCK!
-
Sorry to bother you again, but do you think you could explain why adding "buttons_mc["btn"+i+"_mc"].itemNum=i;" works? Perhaps you can explain the logic behind it?
Thanks deadbeat...love your work
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|