4 Replies Latest reply on Aug 19, 2009 9:18 AM by prodigalmaster

    health += 10; not working (on a button.)

    prodigalmaster Level 2

      Here is my code on a button, when you click the button it takes you to a certain frame and plays, it is meant to add 10 health. When I have it it 52 health, it doesnt go to 62 it goes to 92. When It is at 30 it doesnt go to 40 it goes to 100. why? please help.

       

      on (release) {
          onEnterFrame = function () {
              if (_root.health <=90) {
                  gotoAndPlay("use");
                  _root.health += 10; // I have tried it so this bit of code is in the frame ("use") so it adds 10 health when it lands on that frame, didnt work.
              } ////////////// only pay attention to the above.//////////////
              if (_root.health<=100) {
                  use1._visible = false;
                  useFail1._visible = true;
                  if (_root.usePlay2 == 1) {
                      _root.usePlay = 0;
                  } else {
                      _root.usePlay = 1;
                  }
              }
          };

       

      }

       

      Is it something to do with the onEnterFrame? if I remeove that i get a compile error.

      Thanks.

        • 1. Re: health + = 10; not working on a button.
          Ned Murphy Adobe Community Professional & MVP

          Try putting a trace before and after the += 10 line of code to see what the value of health is.  If you believe that the second conditional can be ignored for this problem, then comment it out.  I am not comvinced it wouldn't affect things since it would execute anytime the first conditional does, but I'm also clueless as to what it's purpose is.

          • 2. Re: health += 10; not working (on a button.)
            NSurveyor Level 2

            You are spot on. It has everything to do with the onEnterFrame.

             

            onEnterFrame is an event handler that is invoked repeatedly at the frame rate of your *.swf. That is, if you have 12 frames per second, in 1 second's time, the onEnterFrame script will run 12 times.

             

            Consider the two instances you have mentioned to understand why it is the onEnterFrame's doing:

            1. Health starts off at 52.
              • 52<= 90, so health = 62.
              • 62<=90, so health = 72.
              • 72<=90, so health = 82.
              • 82<=90, so health = 92.
              • 92>90, so the if condition is never satisfied.
            2. Health starts off at 30.
              • 30<=90, so health = 40.
              • 40<=90, so health = 50.
              • 50<=90, so health = 60.
              • 60<=90, so health = 70.
              • 70<=90, so health = 80.
              • 80<=90, so health = 90.
              • 90<=90, so health = 100.
              • 100>90, so the condition is never satisfied.

             

            Clearly this is not what you want to happen, so removing the onEnterFrame to suffice, and it should not result in a compiler error:

             

            on (release) {
                    if (_root.health <=90) {
                        gotoAndPlay("use");
                        _root.health += 10; // I have tried it so this bit of code is in the frame ("use") so it adds 10 health when it lands on that frame, didnt work.
                    } ////////////// only pay attention to the above.//////////////
                    if (_root.health<=100) {
                        use1._visible = false;
                        useFail1._visible = true;
                        if (_root.usePlay2 == 1) {
                            _root.usePlay = 0;
                        } else {
                            _root.usePlay = 1;
                        }
                    }
            }
            

            Note, this will change the behaviour of the code below the "only pay attention to the above" comment as neither will it be run repeatedly. Then again, that might be want you wanted.

            • 3. Re: health += 10; not working (on a button.)
              prodigalmaster Level 2

              Perfect, thanks mate, Also thanks for explaining it.