2 Replies Latest reply on Jan 22, 2013 7:43 AM by kglad

    make selection buttons using loop

    wfzen Level 1

      I have 4 questions in one screen. 2 questions have Yes/No as choices, 2 questions have Yes/No/NA as choices. Each button has 2 states: regular, rollover; I use location control to display the right state correctly based on user choice. I can hard code each button fine, but would like to include rollover, rollout, and release scripts in a loop. 

       

      With someone's help, I finally got the question numbers right, be able to pick up the instance name (as strings) correctly, but it can't display the right image for each action.

       

      It's feel it's so close but also so far away from getting it right. Please help. Thanks.

      The source file is at: http://dl.dropbox.com/u/30106423/buttons.zip

       

      totalQuestions = 4;

      ButtonsForYes();

      ButtonsForNo();

      ButtonsForNA();

       

       

      C1_1_sel = 1;// for testing only: to have answers selected.

      C1_2_sel = 2;

      C1_3_sel = 2;

      C1_4_sel = 3;

      showAnswers();

      // show current answers

      function showAnswers():Void {

                for (var i:Number = 1; i<=totalQuestions; i++) {

                          if (this["C1_"+i+"_sel"] == 1) {

                                    this["C1_"+i+"_1_RO"]._x = this["C1_"+i+"_1"]._x+3;

                                    this["C1_"+i+"_1_RO"]._y = this["C1_"+i+"_1"]._y+2;

                          } else if (this["C1_"+i+"_sel"] == 2) {

                                    this["C1_"+i+"_2_RO"]._x = this["C1_"+i+"_2"]._x+3;

                                    this["C1_"+i+"_2_RO"]._y = this["C1_"+i+"_2"]._y+2;

                          } else if (this["C1_"+i+"_sel"] == 3) {

                                    this["C1_"+i+"_3_RO"]._x = this["C1_"+i+"_3"]._x+3;

                                    this["C1_"+i+"_3_RO"]._y = this["C1_"+i+"_3"]._y+2;

                          }

                }

      }

       

       

      function resetAll():Void {

                if (selectedQuestion == 1) {

                          C1_1_sel = 0;

                          C1_1_1_RO._x = -1000;

                          C1_1_1_RO._y = -1000;

                          C1_1_2_RO._x = -1000;

                          C1_1_2_RO._y = -1000;

                } else if (selectedQuestion == 2) {

                          C1_2_sel = 0;

                          C1_2_1_RO._x = -1000;

                          C1_2_1_RO._y = -1000;

                          C1_2_2_RO._x = -1000;

                          C1_2_2_RO._y = -1000;

                } else if (selectedQuestion == 3) {

                          C1_3_sel = 0;

                          C1_3_1_RO._x = -1000;

                          C1_3_1_RO._y = -1000;

                          C1_3_2_RO._x = -1000;

                          C1_3_2_RO._y = -1000;

                          C1_3_3_RO._x = -1000;

                          C1_3_3_RO._y = -1000;

                } else if (selectedQuestion == 4) {

                          C1_4_sel = 0;

                          C1_4_1_RO._x = -1000;

                          C1_4_1_RO._y = -1000;

                          C1_4_2_RO._x = -1000;

                          C1_4_2_RO._y = -1000;

                          C1_4_3_RO._x = -1000;

                          C1_4_3_RO._y = -1000;

                }

      }

       

       

       

       

      /* NOTE:

      MC instance names:

      regular state

      q1- yes: C1_1_1, no: C1_1_2

      q2- yes: C1_2_1, no: C1_2_2

      q3- yes: C1_3_1, no: C1_3_2, NA:  C1_3_3

      q4- yes: C1_4_1, no: C1_4_2, NA:  C1_4_3

      rollover state

      add _RO after the names for regular states

       

       

      question answer variables:

      q1 - C1_1_sel yes = 1, no = 2

      q2 - C1_2_sel yes = 1, no = 2

      q3 - C1_3_sel yes = 1, no = 2, NA = 3

      q4 - C1_4_sel yes = 1, no = 2, NA = 3

      */

       

       

      function ButtonsForYes():Void {

                for (var i:Number = 1; i<=totalQuestions; i++) {

                          var btn:MovieClip = this["C1_"+i+"_1"];

                          this["C1_"+i+"_1"].Num = i;

                          btn.onRollOver = function():Void  {

                                    trace("***** RO starts *****");

                                    trace("rollover YES on question "+this.Num);

                                    selectedQuestion = this.Num;

                                    this["C1_"+selectedQuestion+"_1_RO"]._x = this["C1_"+selectedQuestion+"_1"]._x+3;

                                    this["C1_"+selectedQuestion+"_1_RO"]._y = this["C1_"+selectedQuestion+"_1"]._y+2;

                                    trace(["ro image instance: "+"C1_"+selectedQuestion+"_1_RO"]);

                                    trace("x loc: "+["C1_"+selectedQuestion+"_1_RO"]._x);

                                    trace("***** RO ends *****");

                          };

                          btn.onRollOut = function():Void  {

                                    trace("rollout YES on question "+this.Num);

                                    selectedQuestion = this.Num;

                                    if (this["C1_"+selectedQuestion+"_sel"] != 1) {

                                              this["C1_"+selectedQuestion+"_1_RO"]._x = -1000;

                                              this["C1_"+selectedQuestion+"_1_RO"]._y = -1000;

                                    } else {

                                              this["C1_"+selectedQuestion+"_1_RO"]._x = this["C1_"+selectedQuestion+"_1"]._x+3;

                                              this["C1_"+selectedQuestion+"_1_RO"]._y = this["C1_"+selectedQuestion+"_1"]._y+2;

                                    }

                          };

                          btn.onRelease = function():Void  {

                                    trace("***** Press starts *****");

                                    trace("selected YES on question "+this.Num);

                                    selectedQuestion = this.Num;

                                    whichQVar = ["C1_"+this.Num+"_sel"];

                                    resetAll();

                                    //this["C1_resetAll"+i+"()"];// call function to reset locations

                                    this["C1_"+selectedQuestion+"_1_RO"]._x = this["C1_"+selectedQuestion+"_1"]._x+3;

                                    this["C1_"+selectedQuestion+"_1_RO"]._y = this["C1_"+selectedQuestion+"_1"]._y+2;

                                    this[whichQVar] = 1;

                                    trace("question variable name to set: "+whichQVar);

                                    trace("answers q 1 to 4: "+C1_1_sel+" "+C1_2_sel+" "+C1_3_sel+" "+C1_4_sel);

                                    trace("***** Rress ends *****");

                          };

                }

      }

       

       

      function ButtonsForNo():Void {

                for (var i:Number = 1; i<=totalQuestions; i++) {

                          var btn:MovieClip = this["C1_"+i+"_2"];

                          this["C1_"+i+"_2"].Num = i;

                          btn.onRollOver = function():Void  {

                                    trace("***** RO starts *****");

                                    trace("rollover No on question "+this.Num);

                                    selectedQuestion = this.Num;

                                    this["C1_"+selectedQuestion+"_2_RO"]._x = this["C1_"+selectedQuestion+"_2"]._x+3;

                                    this["C1_"+selectedQuestion+"_2_RO"]._y = this["C1_"+selectedQuestion+"_2"]._y+2;

                                    trace(["ro image instance: "+"C1_"+selectedQuestion+"_2_RO"]);

                                    trace("x loc: "+["C1_"+selectedQuestion+"_2_RO"]._x);

                                    trace("***** RO ends *****");

                          };

                          btn.onRollOut = function():Void  {

                                    trace("rollout No on question "+this.Num);

                                    selectedQuestion = this.Num;

                                    if (this["C1_"+selectedQuestion+"_sel"] != 2) {

                                              this["C1_"+selectedQuestion+"_2_RO"]._x = -1000;

                                              this["C1_"+selectedQuestion+"_2_RO"]._y = -1000;

                                    } else {

                                              this["C1_"+selectedQuestion+"_2_RO"]._x = this["C1_"+selectedQuestion+"_2"]._x+3;

                                              this["C1_"+selectedQuestion+"_2_RO"]._y = this["C1_"+selectedQuestion+"_2"]._y+2;

                                    }

                          };

                          btn.onRelease = function():Void  {

                                    trace("***** Press starts *****");

                                    trace("selected No on question "+this.Num);

                                    selectedQuestion = this.Num;

                                    whichQVar = ["C1_"+this.Num+"_sel"];

                                    resetAll();

                                    this["C1_"+selectedQuestion+"_2_RO"]._x = this["C1_"+selectedQuestion+"_2"]._x+3;

                                    this["C1_"+selectedQuestion+"_2_RO"]._y = this["C1_"+selectedQuestion+"_2"]._y+2;

                                    this[whichQVar] = 2;

                                    trace("question variable name to set: "+whichQVar);

                                    trace("answers q 1 to 4: "+C1_1_sel+" "+C1_2_sel+" "+C1_3_sel+" "+C1_4_sel);

                                    trace("***** Rress ends *****");

                          };

                }

      }

        • 1. Re: make selection buttons using loop
          wfzen Level 1

          I've not figured it out. I don't fully understand how "this" is used. Can you tell me in function ButtonsForYear() modified below, why:

           

          1. MC_RO and MC_Reg inside btn.onRollOver would get "undefined" but if removing "this" it gets the name correctly but it won't do anything.

          2. Move MC_RO and MC_Reg to the top, it would get _level0.C1_1_1_RO, _level0.C1_1_1,  etc correctly, but it loops though and ends at the last one instead of individual MC the user is rolling over.

           

          So I guess if I can make MC_RO (for rollover images) and MC_Reg shows _level0.C1_<variable>_1 inisde btn.onRollOver correctly, I can make it work. Please help!

           

          for (var i:Number = 1; i<=totalQuestions; i++) {

            var btn:MovieClip = this["C1_"+i+"_1"];

            this["C1_"+i+"_1"].Num = i;

            selectedQuestion = i;

            MC_RO = this["C1_"+selectedQuestion+"_1_RO"];

            MC_reg = this["C1_"+selectedQuestion+"_1"];

            trace(selectedQuestion+" "+MC_RO+" "+MC_reg);

              btn.onRollOver = function():Void  {

                trace("***** RO starts *****");

                trace("rollover YES on question "+this.Num);

                selectedQuestion = this.Num;

                MC_RO = this["C1_"+selectedQuestion+"_1_RO"];

                MC_reg = this["C1_"+selectedQuestion+"_1"];

                trace(MC_RO);

                trace(MC_reg);

                MC_RO._x = MC_reg._x+3;

                MC_RO._y = MC_reg._y+2;

                trace(["ro image instance: "+"C1_"+selectedQuestion+"_1_RO"]);

                trace("x loc: "+this["C1_"+selectedQuestion+"_1_RO"]._x);

                trace("***** RO ends *****");

              };

          ...

           

           

          Thanks for any suggestions,

          • 2. Re: make selection buttons using loop
            kglad Adobe Community Professional & MVP

            above that rollover, "this" references the timeline containing the code.  inside the rollover, "this" references btn.

            1 person found this helpful