3 Replies Latest reply on May 29, 2013 8:04 AM by Zaxist

    Why are my variables off by 1?

    1J7

      This seems so confusingly simple. I have a numerical variable that displays in text on the canvas. There is a button that should increase this variable by 1 and subsequently show the increase.

       

      I.e. The variable shows 0

      Button Press

      The variable shows 1

      Button press

      The variable shows 2

      etc.

       

      However, what happens is this:

       

      The Variable Shows 0

      Button Press

      The variable shows 0

      Button press

      The variable shows 1

      Button press

      The variable shows 2

       

      Why is there this delay with the first click? I am at a loss. Here's the code:

       

      [On timeline.play]

      sym.setVariable("ScoreLeft", 0);

       

      [On button click

       

      var ScoreLeft = sym.getVariable("ScoreLeft");

      sym.setVariable("ScoreLeft", ScoreLeft+1);

      sym.$("ScoreL").html(ScoreLeft)

        • 1. Re: Why are my variables off by 1?
          Zaxist Level 4

          Try This way :

           

          [On timeline.play]

          ScoreLeft = 0;

           

          [On button click]

           

          ScoreLeft++;

          sym.$("ScoreL").html(ScoreLeft)

          • 2. Re: Why are my variables off by 1?
            1J7 Level 1

            That is so much simpler. Can you explain why my original version wasn't working? I'm just curious. I based it off of the tutorial code but clearly the tutorial uses overcomplicated code apparently. (and I've been doing edge/javascript for about a day now and I can't ever get simple explanations of basic code).

            • 3. Re: Why are my variables off by 1?
              Zaxist Level 4

              yes i can explain to you, this is very simple mistake that you have in your codes

               

              you have this line

               

              [On timeline.play]

              sym.setVariable("ScoreLeft", 0);

               

              in this line you create a variable named  ScoreLeft and set 0 value to it

              in this line you have no problems but you have these codes in your button click action

               

              [On button click]

               

              var ScoreLeft = sym.getVariable("ScoreLeft");

              sym.setVariable("ScoreLeft", ScoreLeft+1);

              sym.$("ScoreL").html(ScoreLeft)

               

              Here is the Problem

               

              in the first line

               

              var ScoreLeft = sym.getVariable("ScoreLeft");

               

              you get variable, then in the Second line

               

              sym.setVariable("ScoreLeft", ScoreLeft+1);

               

              you add variable value with One, and now here is where you made mistake

               

              you assigned value to a text by this line

               

              sym.$("ScoreL").html(ScoreLeft)

               

              but this value is 0 !!!

               

              because when you used var ScoreLeft = sym.getVariable("ScoreLeft"); you set the value of ScoreLeft to 0 !! and the ScoreLeft Variable that you are use in next two lines to show the value in your text have 0 value

               

              Let me show you better, i change the ScoreLeft variable with XWZ so you can undrestand better:

               

              you did this Mistake :

               

              [On button click]

               

              var XWZ= sym.getVariable("ScoreLeft");  ///you set XWZ variable to 0 value

              sym.setVariable("ScoreLeft", XWZ+1);

              sym.$("ScoreL").html(XWZ) /// and you show XWZ Value not your ScoreLeft current Value !!

               

              if you want to show your ScoreLeft Current Value you should add this line before Show value in your text

               

              [On button click]

               

              var XWZ= sym.getVariable("ScoreLeft");  ///you set XWZ variable to 0 value

               

              sym.setVariable("ScoreLeft", XWZ+1);

               

              var XWZ= sym.getVariable("ScoreLeft"); // you get Current Value of your ScoreLeft and send it to XWZ Variable

               

              sym.$("ScoreL").html(XWZ) /// and now you show your ScoreLeft current Value !!

               

               

              you made this mistake cause of using same variable name as you used in your SetVariable function

               

              i hope that i could help you to undrestand the problem...

               

              Zaxist