5 Replies Latest reply on Mar 28, 2007 12:14 PM by kglad

    Variable / If Problem

    Surly
      Okay, I'm hoping this is a simple question with a glaring error... I'm new to using variables.

      Basically I have a menu and the first button (could be any button on the menu) to be clicked should go to a label in another movie clip which has a graphic tween to a different color.

      I want this "color change" only to happen the first time a user clicks one of any of these buttons.

      So, I've created a variable on the first frame of the entire movie and set it to "0":

      var x:Number;
      var x = 0;
      trace("x is now " + x);

      I believe I'm good up to this point as the output is fine. Then in the next scene, nested fairly deep in some other movie clips I have my menu and I'm testing this on the first button:

      on (release) {
      if (x = 0) {
      mc_floorplan_color.gotoAndPlay("color_change");
      var x = 1;
      trace("var is " + x);
      }
      }

      It is not working at all. In my mind x is equal to zero at this point and it should follow through with everything below it, setting x to 1 so the color change wont happen on the other buttons.

      I'm pretty sure the problem isn't with going into the other movie clip and getting to the label, I tried pointing it just to start the movie over again and still got no response and also no output.

      Any help would be appreciated, thanks!

        • 1. Re: Variable / If Problem
          kglad Adobe Community Professional & MVP
          you have a scope problem. x is defined on one timeline and flash is checking another for the variable x.

          further, it's not necessary to define x prior to your button being pressed. you can use:

          • 2. Re: Variable / If Problem
            Surly Level 1
            Thanks for the help.

            They are true buttons, each just moves the timeline to a label. Your code works and there don't seem to be any issues. It changes color on only the first button pressed.

            I've tried defining x in the second scene my mc is in as well as within the mc itself, still no luck getting my previous code to work. Good to know that its unnessesary but I wish I understood it better. I always thought the proper way to do it was to define variables in the first frame of the first scene of the entire movie, not correct?

            Can you briefly walk me through how yoru code is working? Just the second and fourth lines are unclear to me on how it works (not a programmer by any means).

            How would I code it if I wanted to use x as a variable? I guess I don't see why it is not working now that I've defined the variable on the same timeline.

            Thanks again.

            • 3. Re: Variable / If Problem
              Surly Level 1
              To clarify I now have in a frame in the mc:

              var x:Number;
              var x = 0;
              trace("x is now " + x);

              and on the layer below it a button with the code:

              on (release) {
              if (x = 0) {
              mc_floorplan_color.gotoAndPlay("color_change");
              var x = 1;
              trace("var is " + x);
              }
              }

              and its not working. I'm sure the path to "color change" is fine because it works with your code. Thanks.
              • 4. Re: Variable / If Problem
                kglad Adobe Community Professional & MVP
                because you have true buttons and (i assume) more than one button on the timeline containing your buttons, you can use:

                • 5. Re: Variable / If Problem
                  kglad Adobe Community Professional & MVP
                  no, that won't work unless you change your buttons to movieclip buttons.