A Flash Developer Resource Site

Results 1 to 16 of 16

Thread: For Loops using i

  1. #1
    Get Squared Away.
    Join Date
    Feb 2000
    Location
    Allentown, PA
    Posts
    543

    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

  2. #2
    Junior Member
    Join Date
    Aug 2004
    Posts
    2
    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 )

  3. #3
    Get Squared Away.
    Join Date
    Feb 2000
    Location
    Allentown, PA
    Posts
    543
    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.

  4. #4
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    Try:

    buttons_mc["btn"+i+"_mc"].onRollOver = function() {

    and

    _root.name_txt.text = this["btn"+i+"_txt"].text;

    K.

  5. #5
    Get Squared Away.
    Join Date
    Feb 2000
    Location
    Allentown, PA
    Posts
    543
    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

  6. #6
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    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.

  7. #7
    Get Squared Away.
    Join Date
    Feb 2000
    Location
    Allentown, PA
    Posts
    543
    Oh okay, thanks.

    but why no dot after "buttons_mc" and "this"??

  8. #8
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    Don't need the dot when using array access...

    K.

  9. #9
    Get Squared Away.
    Join Date
    Feb 2000
    Location
    Allentown, PA
    Posts
    543
    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?

  10. #10
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    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.

  11. #11
    Get Squared Away.
    Join Date
    Feb 2000
    Location
    Allentown, PA
    Posts
    543
    Yes, for some reason it is saying UNDEFINED, and the text rollover is only working for the last button instance (btn20_mc).

  12. #12
    Get Squared Away.
    Join Date
    Feb 2000
    Location
    Allentown, PA
    Posts
    543
    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;

  13. #13
    Get Squared Away.
    Join Date
    Feb 2000
    Location
    Allentown, PA
    Posts
    543
    Having problems calling "i" withinthe onRollOver function, the last line of code...it only remembers "items20"



    PHP Code:
    for (i=1i<21i++) {
     
    //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;
     };


  14. #14
    FK Slacker
    Join Date
    Jun 2000
    Location
    vancouver
    Posts
    3,208
    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.

  15. #15
    Get Squared Away.
    Join Date
    Feb 2000
    Location
    Allentown, PA
    Posts
    543
    Works like a charm...you ROCK!

  16. #16
    Get Squared Away.
    Join Date
    Feb 2000
    Location
    Allentown, PA
    Posts
    543
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center