11 Replies Latest reply on Jul 8, 2009 5:08 PM by Ned Murphy

    increment a variable with a button press?

    Ragemaster
      what is the proper way to increment a variable with a mouse event listener?

      var myMinVarable:int = myNewMinVariable;
      myNewMinVariable = 0;

      var myMinDMinVariable:int = 10
      button1_mc.addEventListener(MouseEvent.CLICK, onClick);
      function onClick(event:MouseEvent):void
      {
      button1_mc.buttonMode = true;
      if (myNewMinVariable < myMinDMinVariable) {
      myNewMinVariable ++;
      }
      else {
      myNewMinVariable = 0;
      }
        • 1. Re: increment a variable with a button press?
          kglad Adobe Community Professional & MVP
          that looks good. but you probably want:

          • 2. Re: increment a variable with a button press?
            Ragemaster Level 1
            it still does an endless loop I have attached more of the code to give a better idea of how I wont to control this switch:
            I wont to increment through each case: with each press of the button and each time I evaluate if I have reached my limit before resetting the variable to "0" to start all over again, the MinDminVarable =10 is how many times I wont to cascade through the switch so I add "1" to "myNewMinVariable" until it is > than MinDminvariable... So why do I get a run a way loop if I use a mouse event listener? should it not do one evaluation at each button press? and this happens before I get to use the mouse event listener? It is evaluating and making the decisions before the button press... I have done the same thing in C and C++ and both logic syntax works fine?


            //var myMinVarable:int = 0 // <-- not sure where you need this
            var myNewMinVariable:int = 0;

            var myMinDMinVariable:int = 10;
            button1_mc.addEventListener(MouseEvent.CLICK, onClick);
            button1_mc.buttonMode = true;
            function onClick(event:MouseEvent):void {
            if (myNewMinVariable < myMinDMinVariable) {
            myNewMinVariable ++;
            } else {
            myNewMinVariable = 0;
            }
            }


            switch (myNewMinVariable) { //decodeMin
            case 0 :
            led_color_mc.gotoAndStop("off");
            ledData_01_mc.gotoAndStop(_10);
            ledData_02_mc.gotoAndStop(_10);
            ledData_03_mc.gotoAndStop(_10);
            ledData_04_mc.gotoAndStop(_10);
            reserved_mc.gotoAndStop("off");
            break;
            case 1 :
            led_color_mc.gotoAndStop("red");
            ledData_01_mc.gotoAndStop(_5);
            ledData_02_mc.gotoAndStop(_10);
            ledData_03_mc.gotoAndStop(_10);
            ledData_04_mc.gotoAndStop(_10);
            reserved_mc.gotoAndStop("off");
            break;
            case 2 :
            led_color_mc.gotoAndStop("yellow");
            ledData_01_mc.gotoAndStop(_1);
            ledData_02_mc.gotoAndStop(_0);
            ledData_03_mc.gotoAndStop(_10);
            ledData_04_mc.gotoAndStop(_10);
            reserved_mc.gotoAndStop("off");
            break;
            • 3. Re: increment a variable with a button press?
              TimSymons Level 1
              Is this code on a frame? If it is and you don't have a stop() action then the movieclip will continue to loop and execute the switch statement over and over again.

              I would think you would want to place the "switch" code into another function and then call that function each time from the MouseEvent.CLICK function.

              Tim
              • 4. increment a variable with a button press?
                TimSymons Level 1
                Sorry it posted twice. Not a big fan of my wireless router. :(
                • 5. Re: increment a variable with a button press?
                  Level 7
                  Is this code on a frame? If it is and you don't have a stop() action then the
                  movieclip will continue to loop and execute the switch statement over and over
                  again.

                  I would think you would want to place the "switch" code into another function
                  and then call that function each time from the MouseEvent.CLICK function.

                  Tim

                  • 6. Re: increment a variable with a button press?
                    Ragemaster Level 1
                    I did put a stop(); at the top and still looped endlesly,, it makes scens that nesting things in functions and then call them in another function. I will try that. I will let all know affter dinner...
                    • 7. Re: increment a variable with a button press?
                      Ragemaster Level 1
                      her is the whole thing. I still cant get it to work?
                      I comented the code and out to try to get a clue on what is happening?
                      so far if a variable is called from a function I get a runaway mess and if I nest the variable to the switch it's ok but cant increment with the button..

                      Take me some ware happy -) starting to get a bit frustrated...

                      stop();
                      /*var decodeMin:Number = 0;
                      var decodeMax:Number = 0;
                      var maxDmin:Number = 10;
                      var maxDmax:Number = 10;*/

                      /*var _0:String = "zero";
                      var _1:String = "one";
                      var _2:String = "two";
                      var _3:String = "three";
                      var _4:String = "four";
                      var _5:String = "five";
                      var _6:String = "six";
                      var _7:String = "seven";
                      var _8:String = "eight";
                      var _9:String = "nine";
                      var _10:String = "ten";
                      var _11:String = "eleven";*/

                      button1_mc.addEventListener(MouseEvent.CLICK, onClick);
                      button2_mc.addEventListener(MouseEvent.CLICK, onClick);

                      function onClick(event:MouseEvent):void
                      {
                      //decodeMin ++
                      trace("click!!!!");
                      //changeDigi();

                      }

                      button1_mc.buttonMode = true;
                      button2_mc.buttonMode = true;

                      // bellow I had to make this table to convert names to fake integers because
                      // flash canot except integers for frame labels? that's stupid....

                      /*
                      decodeMin variable handles the first 4 digits
                      decodeMax varable handles the last 5 digits
                      (_0)= 0, (_1)= 1, (_2)= 2, (_3)= 3,(_4)= 4, (_5)= 5,(_6)= 6,(_7)= 7,(_8)= 8,(_9)= 9,
                      (_10)= off,(_11)= Period,

                      led_color_mc, controles the Lens color and uses string names as shown "all lowercase"
                      ("red") = red
                      ("orange") = orange
                      ("yellow") = yellow
                      ("green") = green
                      ("blue") = blue
                      ("ltblue") = ltblue
                      ("purple") = purple
                      ("turk") = turk
                      ("gold") = gold
                      ("white") = white
                      ("off") = off
                      */

                      //function changeDigi():void{
                      /*switch (decodeMin) {
                      case 0 :
                      led_color_mc.gotoAndStop("off");
                      ledData_01_mc.gotoAndStop(_10);
                      ledData_02_mc.gotoAndStop(_10);
                      ledData_03_mc.gotoAndStop(_10);
                      ledData_04_mc.gotoAndStop(_10);
                      reserved_mc.gotoAndStop("off");
                      break;
                      case 1 :
                      led_color_mc.gotoAndStop("red");
                      ledData_01_mc.gotoAndStop(_5);
                      ledData_02_mc.gotoAndStop(_10);
                      ledData_03_mc.gotoAndStop(_10);
                      ledData_04_mc.gotoAndStop(_10);
                      reserved_mc.gotoAndStop("off");
                      break;
                      case 2 :
                      led_color_mc.gotoAndStop("yellow");
                      ledData_01_mc.gotoAndStop(_1);
                      ledData_02_mc.gotoAndStop(_0);
                      ledData_03_mc.gotoAndStop(_10);
                      ledData_04_mc.gotoAndStop(_10);
                      reserved_mc.gotoAndStop("off");
                      break;
                      case 3 :
                      led_color_mc.gotoAndStop("ltblue");
                      ledData_01_mc.gotoAndStop(_1);
                      ledData_02_mc.gotoAndStop(_5);
                      ledData_03_mc.gotoAndStop(_10);
                      ledData_04_mc.gotoAndStop(_10);
                      reserved_mc.gotoAndStop("off");
                      break;
                      case 4 :
                      led_color_mc.gotoAndStop("green");
                      ledData_01_mc.gotoAndStop(_2);
                      ledData_02_mc.gotoAndStop(_5);
                      ledData_03_mc.gotoAndStop(_10);
                      ledData_04_mc.gotoAndStop(_10);
                      reserved_mc.gotoAndStop("off");
                      break;
                      case 5 :
                      led_color_mc.gotoAndStop("orange");
                      ledData_01_mc.gotoAndStop(_5);
                      ledData_02_mc.gotoAndStop(_0);
                      ledData_03_mc.gotoAndStop(_10);
                      ledData_04_mc.gotoAndStop(_10);
                      reserved_mc.gotoAndStop("off");
                      break;
                      case 6 :
                      led_color_mc.gotoAndStop("white");
                      ledData_01_mc.gotoAndStop(_1);
                      ledData_02_mc.gotoAndStop(_0);
                      ledData_03_mc.gotoAndStop(_0);
                      ledData_04_mc.gotoAndStop(_10);
                      reserved_mc.gotoAndStop("off");
                      break;
                      case 7 :
                      led_color_mc.gotoAndStop("pink");
                      ledData_01_mc.gotoAndStop(_2);
                      ledData_02_mc.gotoAndStop(_0);
                      ledData_03_mc.gotoAndStop(_0);
                      ledData_04_mc.gotoAndStop(_10);
                      reserved_mc.gotoAndStop("off");
                      break;
                      case 8 :
                      led_color_mc.gotoAndStop("purple");
                      ledData_01_mc.gotoAndStop(_5);
                      ledData_02_mc.gotoAndStop(_0);
                      ledData_03_mc.gotoAndStop(_0);
                      ledData_04_mc.gotoAndStop(_10);
                      reserved_mc.gotoAndStop("off");
                      break;
                      case 9 :
                      led_color_mc.gotoAndStop("gold");
                      ledData_01_mc.gotoAndStop(_1);
                      ledData_02_mc.gotoAndStop(_0);
                      ledData_03_mc.gotoAndStop(_0);
                      ledData_04_mc.gotoAndStop(_0);
                      reserved_mc.gotoAndStop("off");
                      break;
                      case 10 :
                      led_color_mc.gotoAndStop("off");
                      ledData_01_mc.gotoAndStop(_10);
                      ledData_02_mc.gotoAndStop(_10);
                      ledData_03_mc.gotoAndStop(_10);
                      ledData_04_mc.gotoAndStop(_10);
                      reserved_mc.gotoAndStop("on");
                      break;


                      }
                      // change the maximum numbers with button2 action
                      switch (decodeMax) {
                      case 0 :
                      ledData_05_mc.gotoAndStop(_10);
                      ledData_06_mc.gotoAndStop(_10);
                      ledData_07_mc.gotoAndStop(_10);
                      ledData_08_mc.gotoAndStop(_10);
                      ledData_09_mc.gotoAndStop(_10);
                      break;
                      case 1 :
                      ledData_05_mc.gotoAndStop(_5);
                      ledData_06_mc.gotoAndStop(_0);
                      ledData_07_mc.gotoAndStop(_0);
                      ledData_08_mc.gotoAndStop(_10);
                      ledData_09_mc.gotoAndStop(_10);
                      break;
                      case 2 :
                      ledData_05_mc.gotoAndStop(_1);
                      ledData_06_mc.gotoAndStop(_0);
                      ledData_07_mc.gotoAndStop(_0);
                      ledData_08_mc.gotoAndStop(_0);
                      ledData_09_mc.gotoAndStop(_10);
                      break;
                      case 3 :
                      ledData_05_mc.gotoAndStop(_2);
                      ledData_06_mc.gotoAndStop(_0);
                      ledData_07_mc.gotoAndStop(_0);
                      ledData_08_mc.gotoAndStop(_0);
                      ledData_09_mc.gotoAndStop(_10);
                      break;
                      case 4 :
                      ledData_05_mc.gotoAndStop(_3);
                      ledData_06_mc.gotoAndStop(_0);
                      ledData_07_mc.gotoAndStop(_0);
                      ledData_08_mc.gotoAndStop(_0);
                      ledData_09_mc.gotoAndStop(_10);
                      break;
                      case 5 :
                      ledData_05_mc.gotoAndStop(_5);
                      ledData_06_mc.gotoAndStop(_0);
                      ledData_07_mc.gotoAndStop(_0);
                      ledData_08_mc.gotoAndStop(_0);
                      ledData_09_mc.gotoAndStop(_10);
                      break;
                      case 6 :
                      ledData_05_mc.gotoAndStop(_1);
                      ledData_06_mc.gotoAndStop(_0);
                      ledData_07_mc.gotoAndStop(_0);
                      ledData_08_mc.gotoAndStop(_0);
                      ledData_09_mc.gotoAndStop(_0);
                      break;
                      case 7 :
                      ledData_05_mc.gotoAndStop(_2);
                      ledData_06_mc.gotoAndStop(_5);
                      ledData_07_mc.gotoAndStop(_0);
                      ledData_08_mc.gotoAndStop(_0);
                      ledData_09_mc.gotoAndStop(_0);
                      break;



                      }*/
                      //}

                      • 8. Re: increment a variable with a button press?
                        TimSymons Level 1
                        Not sure what each of your movieclips are doing but to simplify the incrementing of the variable test, I took out most of your MCs and just put in a trace statement. The following code works by clicking on either of button2_mc or button1_mc. The output window will show you wihich case statement was executed. You will obviously need to put back in the conditionals that kglad showed earlier and your other code. My recommendation would be to add back in the other code in small chunks. You may find that your infinite loop is in a different location than you first thought.

                        Tim
                        • 9. Re: increment a variable with a button press?
                          Ragemaster Level 1
                          Ok that's working
                          but it looks like I was declaring the same variable twice and was canceling out the other so there was no value set for the variable decodeMin Hmm! in C the compiler will always set 0 and the loop function should not have worked at all.... in Flash it seams the last object on is the first object exicuted..
                          • 10. Re: increment a variable with a button press?
                            zm15 Level 1

                            do you have a link to this project? I'm interested in seeing what it looks like!

                            • 11. Re: increment a variable with a button press?
                              Ned Murphy Adobe Community Professional & MVP

                              It is doubtful you are going to get the attention of the poster .... check the date of the discussion.