8 Replies Latest reply on Jul 12, 2006 5:17 PM by

    Learning ActionScript - Var declarations

      I'm trying to learn ActionScript, and while the fog is starting to lift there are little things like this that confuse me.

      I have an existing audio player project that I'm revising according to the Best Practices docs.

      In the first frame of the Action layer it has a script that includes variable declarations like this:

      this.autoStart = 0;

      It appears this code declares a variable called autoStart and sets its initial value to zero. In the variable list window at runtime it comes up as:

      Variable _level0.autoStart = 0

      So It's working as it is, but in reading the documentation it appears it would be better to write the code like this:

      var this.autoStart:Number = 0;

      However that doesn't work. It generates an error when I try to test the movie.

      If I change it to:

      var autoStart:Number = 0;

      It works.

      All I've done is remove the "this." from the declaration.

      I'm trying to understand why the notation this.autostart works without the var keyword, but not with the var keyword.

      Thanks!

      Tony


        • 1. Re: Learning ActionScript - Var declarations
          Level 7
          Tony,

          > in reading the documentation it appears it would
          > be better to write the code like this:
          >
          > var this.autoStart:Number = 0;
          >
          > However that doesn't work. It generates an error
          > when I try to test the movie.

          The var statement only works with non-pathed references to a variable.
          You can write this ...

          var autoStart:Number = 0;

          ... and the variable will be declared and scoped to whatever timeline holds
          this script. If you're in the main timeline, the above is essentially the
          same as writing ...

          this.autoStart = 0;

          ... as long as "this" refers to that same timeline. Really, var handles
          scope, which determines "where" an object resides. If you happen to have a
          movie clip in the main timeline with the instance name mc, and if you want
          mc to contain this autoStart variable, you could either do this ...

          // A)
          // in a script in the main timeline
          mc.autostart = 0;
          // or
          this.mc.autoStart = 0;

          // or B)
          // in the timeline of mc
          var autoStart:Number = 0;

          All three of those are basically equivalent, because all three of those
          scopes the autoStart variable to mc. Note that the only time you can use
          strong typing (:Number) is with the var statement.


          David
          stiller (at) quip (dot) net
          Dev essays: http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: Learning ActionScript - Var declarations
            David,

            The situation in this audio player is that there is a variable autoStart that needs to be read by scripts that are part of movieclips that are on other layers of the main timeline.

            It seems like the declaration:

            var autoStart:Number = 0;

            Creates a "timeline" variable.

            If I understand things correctly a timeline variable's scope allows it to be accessed by any script on that same timeline.

            This existing app has a symbol called emptyMC, three instances of which were placed outside of the stage. Each of them is on their own layer which is part of the main timeline. On the left side of the Action panel there are symbol definitions. The symbol emptyMC has two short scripts. One on the first frame and one on frame number 5.

            I understand how the scripts inside the symbol work. I don't understand why the developer used 3 instances of the same symbol (but maybe that's another question). Each of these symbols has the same timeline, so again, if I understand what I'm looking at it seems like those same scripts could be executed in 3 places.

            Maybe it doesn't matter?

            The way the app is designed only one of the 3 instances of emptyMC is ever called with gotoandplay(), and the script on the first frame of each clip just says stop();, so it appears the script on frame 5 only ever gets executed from one of the instances of emptyMC when it is called with:

            _root.timingMc.gotoAndPlay(2);

            However, each of the 3 instances of emptyMC does have a different script listed under Scene1 in the action panel, and all of those scripts _are_ executed.

            If I click on them in the left side of the action panel (where they are listed under Scene1) they each have different scripts. I'm not sure I understand where those scripts exist (are they timeline scripts that are not on a frame?, or what?). If I click on the first frame next to a layer containing one of the emptyMC clips no script is displayed The main timeline only has one frame.

            It seems like they must be timeline scripts because the they are able to access the timeline variable autoStart that we were originally discussing, but that's a little confusing since the scripts don't appear to be connected to a frame on any timeline.

            BTW, I've seen many of your answers on the forum already, and I found my way to your site over the weekend. Your blog posts are very helpful!

            Thanks!

            Tony
            • 3. Re: Learning ActionScript - Var declarations
              Level 7
              Tony,

              > It seems like the declaration:
              >
              > var autoStart:Number = 0;
              >
              > Creates a "timeline" variable.

              Yes. You can certainly call it that, and many people (including myself)
              do. But all timelines are "actually" movie clip instances. Even the main
              timeline, aka _root, is a movie clip. So if you're inclined toward
              object-oriented programming, that may help you visualize what's going one.

              > If I understand things correctly a timeline variable's
              > scope allows it to be accessed by any script on that
              > same timeline.

              True. Specifically, any script on that timeline that *follows* the
              declaration of the variable. In other words, you can't declare a variable
              on frame 10 and access it on frame 1.

              > This existing app has a symbol called emptyMC, three
              > instances of which were placed outside of the stage.
              > Each of them is on their own layer which is part of the
              > main timeline.

              I'm with ya.

              > On the left side of the Action panel there are symbol
              > definitions. The symbol emptyMC has two short scripts.
              > One on the first frame and one on frame number 5.

              Okay. That means all three intances of it have those frame scripts.

              > I understand how the scripts inside the symbol work.
              > I don't understand why the developer used 3 instances
              > of the same symbol

              Heh, I'm not sure either. ;) Online samples aren't always correct, of
              course. Maybe this one is; maybe it isn't.

              > Each of these symbols has the same timeline, so again,
              > if I understand what I'm looking at it seems like those
              > same scripts could be executed in 3 places.

              There's really only *one* symbol. The three you're mentioning are three
              instances of that one symbol. Yes, the symbol has the same timeline, but
              each instances has its own version of that timeline. Instance A may be on
              frame 1; instance B may be on frame 5, which executes new ActionScript, etc.

              But yes, those scripts could indeed be executed in three places.

              > Maybe it doesn't matter?

              I have no idea! ;)

              > The way the app is designed only one of the 3
              > instances of emptyMC is ever called with gotoandplay(),
              > and the script on the first frame of each clip just says
              > stop();, so it appears the script on frame 5 only ever gets
              > executed from one of the instances of emptyMC when it
              > is called

              That's what it sounds like. From what you describe, this is classic
              Flash 5/Flash MX era programming, where timeline frames were used as
              triggers. This is the sort of programming that Java and C# folks look at
              while scratching their heads.

              > However, each of the 3 instances of emptyMC does
              > have a different script listed under Scene1 in the action
              > panel, and all of those scripts _are_ executed.

              It could be that those scripts are different: if those are attached to
              the clips themselves, each could be unique. This sort of things makes the
              Java and C# people start to cry, actually. You can see if the code is
              directly attached by selecting each clip in turn and keeping an eye on the
              Actions panel.

              > If I click on them in the left side of the action panel
              > (where they are listed under Scene1) they each have
              > different scripts. I'm not sure I understand where those
              > scripts exist

              Bingo. Yeah, those are *on* the clips, rather than in them.

              > It seems like they must be timeline scripts because the
              > they are able to access the timeline variable autoStart
              > that we were originally discussing, but that's a little
              > confusing since the scripts don't appear to be connected
              > to a frame on any timeline.

              It *is* confusing, and not really the way things are done nowadays.
              Click on the clips themselves and see if you can make sense of it. It may
              help you to realize that variables can be seen even by other timelines. All
              you have to do is path to the object in question.

              > BTW, I've seen many of your answers on the forum
              > already, and I found my way to your site over the
              > weekend. Your blog posts are very helpful!

              Thanks, Tony! It's encouraging to hear that. I also contribute to
              communitymx.com (you'll see occasional links from my blog) ... plenty to
              explore over there, too. It's a subscription site, which may be worth your
              while, but they also have free content.


              David
              stiller (at) quip (dot) net
              Dev essays: http://www.quip.net/blog/
              "Luck is the residue of good design."


              1 person found this helpful
              • 4. Re: Learning ActionScript - Var declarations
                Rothrock Level 5
                tonyoppenheim watch out for that David Stiller – he may seem calm, but he is really a seething cauldron! :) (PS that is just a long-running joke!)

                The only thing I wanted to add about var this.someName generating an error. var says make this variable local to this timeline so having any kind of dot syntax after a var doesn't make sense.
                1 person found this helpful
                • 5. Re: Learning ActionScript - Var declarations
                  quote:

                  Originally posted by: Rothrock
                  tonyoppenheim watch out for that David Stiller – he may seem calm, but he is really a seething cauldron! :)



                  Yes, I'm seeing that energy... :)

                  I went back and re-read some of the docs on var again, and I see where I went wrong there.

                  I've been reading the best practices docs on ActionScript and they talk a lot about always using this. and always explicitly typing variables.

                  This is just a really good example of how a little (very little) knowledge can be dangerous! :)

                  Thanks!

                  Tony

                  • 6. Re: Learning ActionScript - Var declarations
                    David,

                    Thanks again!

                    quote:

                    It *is* confusing, and not really the way things are done nowadays.
                    Click on the clips themselves and see if you can make sense of it.

                    In my effort to go for "best practice" coding, I'm trying to move these scripts from those emplyMC clips to the action layer of the timeline. If nothing else It's a good way to learn.

                    I think it will be much easier to understand how things work once I've completed this. In the process I've already found and fixed several bugs that were probably related to things being so scattered.

                    quote:

                    It may help you to realize that variables can be seen even by other timelines. All you have to do is path to the object in question.

                    Yes, in the case of these vars they are accessed as _root.varName in the code that attached to the outside of those emptyMC clips.

                    quote:

                    In other words, you can't declare a variable on frame 10 and access it on frame 1.

                    Execution order is one of the other things I'm trying to get a handle on. I put trace statements in the different scripts to see what was being run when.

                    I'm getting it, slowly, but it's starting to sink in.

                    Thanks again!

                    Tony
                    • 7. Re: Learning ActionScript - Var declarations
                      Rothrock Level 5
                      Just something that I find to be in my "best practices" maybe not official, but you mention _root.varName and _root is one of my pet peeves.

                      If the swf you are authoring ever needs to be loaded into another the _root will change. _root is an absolute reference, so just like if you put a folder inside another folder its absolute path will changed. So after being burned several times (hey you only have teach me 3 or 4 times!) I personally stopped using _root at all! Instead I almost always use relative paths – sometimes if I'm having a difficult time with scope I will use _root as a temporary patch to be sure the problem is with the scoping and not something else.

                      So if the empty clips are on the main timeline and they need to reference their parent timeline I would use _parent.myVarName.
                      1 person found this helpful
                      • 8. Re: Learning ActionScript - Var declarations
                        Rothrock,

                        I agree with you. I'll be changing all those instances of _root that I can figure out.

                        Wouldn't those change to this._parent.myVarName?

                        Or is the this. optional?

                        Also, because I'm relocating the code from the movieclips into the main timeline I won't even need the path for many of the variables which are timeline variables anyway. I think.

                        We'll see.

                        Thanks!

                        Tony