7 Replies Latest reply on Jan 8, 2010 7:44 AM by Limon26

    global variable not targeting

    Limon26 Level 1

      I am trying to set up a global variable.

      On the root of my timeline, I have this;

       


      _global.floorVar = "0";

      trace(floorVar);

       

      which seems to trace fine to say 0.

       

      Then, inside of a movie clip on the main timeline, I have a button function:

       

       

      floor1.onRelease = function() {
      _global.floorVar = "1";
      trace(_global.floorVar)

      _root.gotoAndStop(2);
      };

       

      if (_global.floorVar == 1) {
      trace("it works");

      }

       

       

      It traces as 1 once I press the button, but the if statement doesn't trace "it works"

      is my global variable set up wrong?

        • 1. Re: global variable not targeting
          RossRitchey Level 4

          You global variable is set up as a string, so the if statement needs to check for "1" and not 1.

           

          If you want the var to be a number remove the quotes from around it when you set it:

           

          _global.floorVar = 0;

          • 2. Re: global variable not targeting
            Limon26 Level 1

            I removed the all the quotes so when I call it I say:

             

            _global.floorVar = 0;

             

            The if statement is:

             

            if (_global.floorVar == 1) {

                 trace("sweet");

             

            }

             

            And the button function is:

             

            floor1.onRelease = function() {

                 _global.floorVar = 1;

                 trace(_global.floorVar)

                  

                 _root.gotoAndStop(2);

            };

             

            Is that set up correctly now?

            The if statement is still not tracing "sweet" when the button is pressed to set it as 1?

            • 3. Re: global variable not targeting
              RossRitchey Level 4

              Where is the if statement located?

              • 4. Re: global variable not targeting
                Limon26 Level 1

                The root timeline is where the global variable is declared

                Inside of a movie clip on the main timeline is where the

                Button function is and the if statement is right under the button function.

                • 5. Re: global variable not targeting
                  Rothrock Level 5

                  Where is your if statement. From how it looks here it is something that is just going to execute once, probably right after the _global variable is first defined. If statements don't just hang around and wait until the variable changes.

                   

                  You can add a trace,

                   

                  if (_global.floorVar == 1) {

                       trace("sweet");

                  } else {

                       trace("Darn, at this point floorVar is: "+_gobal.floorVar);

                  }

                   

                  I imagine you will see that trace just once and its value will be zero.

                   

                  If you want to keep checking you can use an enterframe:

                   

                  this.onEnterFrame=function(){

                   

                  if(_global.floorVar == 1){

                  trace("sweet");

                  delete this.onEnterFrame;

                  }

                  }

                   

                  Of course depending upon what you are doing that may or may not be a good way of doing it. There is also the ability to watch() a variable and have a function called anytime the variable is changed—the equivalent of a getter/setter. I don't think I've used it with _global, but I think it should work. Of course _global variables aren't generally good practice....

                  • 6. Re: global variable not targeting
                    Limon26 Level 1

                    Ok, I just moved the if statement within the button function and it works.

                    So is there a better way to do this or a way to combine all the button functions somehow?

                     

                    I have a bunch of button functions that look like this, but the if statement is going to start getting long because there are a bunch of floors that I need to check for:

                     

                    floor6.onRelease = function() {

                         _global.floorVar = 6;

                         _root.gotoAndStop(4);

                    if (_global.floorVar == 1) {

                         do this

                    } else {

                         Do this stuff

                    }

                     

                    };

                     

                    Thanks!

                    • 7. Re: global variable not targeting
                      RossRitchey Level 4

                      You can move that if statement to a separate function:

                       

                      floor6.onRelease = function() {
                           _global.floorVar = 6;
                           _root.gotoAndStop(4);
                      
                           _root.checkFloor();
                      }
                      
                      function checkFloor(){
                           if (_global.floorVar == 1) {
                                do this
                           } else {
                                Do this stuff
                           }
                      }
                      
                      

                       

                      Though, at the same time - if each button press sends to a specific floor, and you are doing something specific for each floor, you can simply put that code in the button press function:

                       

                      floor6.onRelease = function() {
                           _global.floorVar = 6;
                           _root.gotoAndStop(4);
                      
                           //Do the Floor 6 specific stuff//
                      }