3 Replies Latest reply on Sep 14, 2006 7:38 AM by Rothrock

    volatility of variable scope

    TomBisciglia
      I am confused about what seems, on the surface, to be a huge limitation of variables in ActionScript. I'm looking at the following statement from documentation on creating variables:

      "If a variable is declared in the FLA (instead of in an external file), it works only for the frame where the variable is declared. Only the frame code for that frame is scanned, so the second frame would not recognize the variable if it was referenced."

      So this means that there's no way to store a value in a variable and use it later in the movie, unless I create a separate code file? That seems ridiculous. Let's say I want someone to type in their name at the beginning of a movie; I can't store that value in a variable and recall it later in the movie to address the person by name? I can't possibly imagine that we would be forced to do everything in external files just to have a variable that actually stores something for longer than a frame. Can anyone set me straight on this?
        • 1. Re: volatility of variable scope
          Rothrock Level 5
          Of course that is what you get for reading the documentation! :) If you read the next sentence it says, "Initializing a variable helps you track and compare the variable’s value as the SWF file plays." So that seems to contradict the sentence you have quoted.

          So I think this is a case of technical writers having something really important to share, but a serious lack of understanding of the English language. I really have no idea what they are trying to tell you, but I think it has to do with the importance of declaring variables and the change between Flash 6 and 7.

          In any event you seem to be overlooking a REALLY important point. It is all well and good to ask questions here, but you could easily have answered this yourself far quicker than posting here and waiting for an answer.

          Make a new file.
          Add a keyframe at frame two or 30 or what have you.
          Declare a variable on Frame 1.
          Trace the variable on whatever next frame you added.

          What happens?
          • 2. Re: volatility of variable scope
            TomBisciglia Level 1
            Actually, I have had really bizarre problems in the past declaring a variable within an FLA file and then having it be completely invisible elsewhere in the movie. I've tried creating it all sorts of different ways, using _root and this and whatever else, and gone nuts trying to figure out the issue. So my guess is there was something extra-bizarre going on either in my copy of Flash or in the way I built the file.

            In either way, I also submitted this message on the livedocs area itself, so if the documentation is incorrect or misleading, at least they'll know. I write documentation for a living myself - it's a real headache keeping up with all the changes that happen constantly.
            • 3. Re: volatility of variable scope
              Rothrock Level 5
              You shouldn't flail around with _root, this, and whatever else. You should develop a strategy for making sure variables are where they need to be. I'm guessing it isn't your copy of Flash. :)

              If it is a variable that everything needs, most likely a _global variable would be a good choice. If you are keeping it within this object or that movie clip, but can't find it when needed, use the Debug–>List variables and it will show you where everything is.

              It sometimes does get tricky when trying to combine Actionscript with timeline stuff. Personally I try and limit the amount of playhead jumping around within my files. If I do run into a problem, the best approach I've found is to set aside my current project and try and develop the simplest model of the problem. Then I can see how Flash works and how I can get what I need. In the end, regardless of what the documentation says or how I want it to work, Flash does what it does.

              I'm sure they are trying to tell us something important, but I don't know what it is from those sentences. Perhaps that if a variable is defined within an object, like a movieclip and if that movieclip doesn't exist at a later frame, then you won't be able to get the variable? I really don't know.