3 Replies Latest reply on Apr 10, 2007 9:50 PM by Rothrock

    Variable comparison problem

    symmetricalMan Level 1
      Actionscript newbie.

      I have three variables that I call as follows:
      _global johnAlt = false;
      _global garyAlt = false;
      _global tonyAlt = false;

      I also have three buttons, each of which changes the value of each variable, like:
      on (release) {tonyAlt = true;}

      Later, I need to compare the three values, so I've written code like this:
      if ((johnAlt = true) && (garyAlt = true) && (tonyAlt = true))
      {gotoAndPlay(3);}

      else {stop();}

      It doesn't work.
      I've tried using all sorts of combinations of code, including using double ==.

      The only way I got it to work was this way:
      if ((johnAlt = garyAlt) && (garyAlt = tonyAlt) && (tonyAlt = johnAlt))

      ...which essentially is what I'm asking all along, because I'm testing to see when they're all 'true'. But, what if I was trying to compare specific values, like
      if ((car=red) && (gofaster=stripes) && (bigbutton=off))?

      I've checked the Flash help, and various forums, and from what I can see, my code is suppsoed to work.

      Any ideas?
        • 1. Re: Variable comparison problem
          Rothrock Level 5
          When comparing you need to use the comparison operator not the assignment operator. Wow, that sentence, while correct, is probably almost no help! :)

          Evidently when you checked Flash help and various forums you didn't look too closely!

          if(something == somethingelse)

          Notice that there are two equal signs in there. Not just one. And hey I've been doing this for a long, long time and sometimes I still accidentally type only the one and can't figure out what is going wrong.

          However since the values you are comparing are Boolean values (true or false) you can do a neat simple trick:

          if (johnAlt && garyAlt && tonyAlt){
          //do something
          }

          • 2. Re: Variable comparison problem
            symmetricalMan Level 1
            Thanks, but I'd actually started by using the == (as I mentioned in my post).

            The irony is, the typo appeared when I created the post. :) Ha ha!
            What I should have said was that I’d got the code working by writing:
            if ((johnAlt == garyAlt) && (garyAlt = =tonyAlt) && (tonyAlt == johnAlt))

            The sad thing is that when I write this:
            if ((johnAlt == true) && (garyAlt = =true) && (tonyAlt == true))

            …it doesn’t work. That’s my dilemma. Surely that code should give me the outcome I want.
            • 3. Re: Variable comparison problem
              Rothrock Level 5
              From your post it seems that you are sort of flailing about hoping to hit on something correct. There is no correct answer that will use = instead of == so that is part of it.

              My guess is the one that you have working is because they all equal each other and that is undefined or something.

              I'm noticing something else now. Where you change from the false to true. It should be:

              on (release) {_global.tonyAlt = true;}

              Anytime you want to assign a _global value you should use the _global. Otherwise you will be creating a local variable with the same name and it will "mask" the global from some scopes.

              The next thing to test is to see what values your variables have. So right before the if statement add the following code:

              trace("global john is "+_global.johnAlt+" and john is "+johnAlt);
              trace("global gary is "+_global.garyAlt+" and gary is "+garyAlt);
              trace("global tony is "+_global.tonyAlt+" and tony is "+tonyAlt);

              See what values your _global variables have and if any local "masking" variables have been created by accident.

              I promise that if you are correctly assigning your variables that both the one you think is correct and my shorthand version will work.

              So if you have either of those and it isn't working the problem lies elsewhere.