2 Replies Latest reply on Apr 4, 2012 8:51 PM by rantexfire

    how do i increase an integer

    rantexfire

      I'm tring to increase the valueof an integer by clicking a botton. the idea is this,

      a = 1

      a = a + 5

       

      so now

      a = 6

       

      each time hit button the value goes up by 5

      my code is this

       

      var myNum = 1;

       

      myButton.addEventListener (MOUSEEvent.CLICK, myIncreaseHandler)

      function myIncreaseHandler (evt ){

           var myNum = myNum + 5;

           myText.text = myNum.toString();

      }

       

      but the number never increases

        • 1. Re: how do i increase an integer
          Mark Jawdoszak Level 1

          You're creating a new variable every time the button is clicked - so that new variable is being updated (probably saying "5" right?), not the global variable.

           

          Within your event handler, you want to put:

          function myIncreaseHandler( Event:event )

          {

          myNum += 5;

          myText.text = myNum.toString();

          }

           

          By ommiting the "var" portion, you are using the "var myNum = 1;" that you declared outside of the function.  Also, the "+= 5" part saves you a lot of re-writing, as this is an incrementor, and simply adds the next value to the existing value.

           

          Hope that helps!

          1 person found this helpful
          • 2. Re: how do i increase an integer
            rantexfire Level 1

            Yeah that helped. I ended up having to use myText.text is the integer start point instead of myNum =0;