3 Replies Latest reply on Jan 11, 2010 7:33 AM by Limon26

    Comparing two variables with if statements

    Limon26 Level 1

      I'm working with 2 global variables and I need to compare them on 6 different button function clicks for a lot of different scenarios.

      Here is an example of what I have for 1 button:


      my Btn.onPress = function() {
      _global.firstVar = 19;
      _global.firstYear =2009;


      My function is starting to look like this;


      if ((_global.firstVar == 19) && (_global.firstYear == 2009)){     (first...is this the correct way to write this statement with the && to compare?)
        loadMovie("mymovie.swf", 1);


      else if..........


      only problem i see is that there are many different scenarios and this if statement could get very long.

      Is there a way to combine it somehow so there wouldn't be that many else if's?

        • 1. Re: Comparing two variables with if statements
          Ned Murphy Adobe Community Professional & MVP

          What you show appears to be correct assuming you want both 19 and 2009 to be the values.  As far as efficiently coding the conditions, that all depends on the conditions themselves.  If some conditions would be the same for different scenarios (like 2009 happens more than once), then they could be grouped to have the shared condition tested first, then test the partner conditions.  But that's just guessing at what goes on with your conditions... there may be an even cleaner solution if a mathematical approach suits the scenario.


          I would recommend using onRelease rather than onPress, unless you have some reason for holding the button down.

          1 person found this helpful
          • 2. Re: Comparing two variables with if statements
            Rothrock Level 5

            That is most likely the way to write it, but I have the feeling that you are making it much more complex than it needs to be. What are you actually trying to do?


            How are firstVar and firstYear being set? Can they be any number at all or just some specific ones?


            Using _global variables isn't really a best practice and should only be done the most unusual of circumstances. There are some tricky things about the _global object. Generally they are just a shortcut for getting around scope problems, but if you have those problems it is better to work them out and understand scope that to just put everything in _global. In AS3, the _global object has gone away—unless you make your own....


            Also depending upon what you are doing with the loading of the swf, you probably should look into using the MovieClipLoader instead of a loadMovie. With loadMovie there is no easy way to know when the clip has finished loading, unless you write your own preloader code. With the MovieClipLoader class there is a nice simple event called onInit. You can also use the onLoadProgress event to know how much of your external asset has loaded.


            Also using gotoAndStop(8) or some other number is likely to become confusing later down the line. You might want to use labels and names. If you have a lot of different scenarios then maybe give them names. That way if the timeline changes or you come back in 3 months it will generally be easier to track down:




            rather than




            Just a suggestion from years' of experiences.


            And finally I'm with Ned. Generally for buttons onRelease is a better event to do navigation and other results things that happen upon a click. onPress is really for things where you need to press and hold the button.

            1 person found this helpful
            • 3. Re: Comparing two variables with if statements
              Limon26 Level 1

              I just started a new post with an updated function I wrote, can you please take a look at that, hopefully I'm making it less complex now...?