This tutorial is meant for Flash developers who are just starting to get a handle on scope. What follows is the simplified version. Also, this isn’t about how code on one MovieClip talks to code on another MovieClip and so forth. We’re just talking about code on one frame here.
With that said here are a few analogies that might help you understand scope.
First off lets start with a simple definition:
scope = the range over which a variable is defined.
Think of your code as a landscape. When you create a variable it has an existence in a specific place, like say down in a valley. Lets call this variable downNvalley and give it a value of, say, 3. Now if you climb up on a mountain - create a function - and you are overlooking the valley you can still see your variable. You can even yell out to it and say, “hey, Mr. downNvalley, can you add 1 to your value?”, or something like that.
That code would look like this:
output:
from climbMountain: 3
Now suppose while you’re up on the mountain - inside the function - you create a variable. We’ll call it upOnMountain and give it a value of “just smell the cool, clean, air“. As long as you are on the mountain you can still see your new variable. But if you go back down into the valley you won’t be able to see it. If you ask anyone around you, “hey, do you know Mr. upOnMountain?” They’ll say, “Nope, never heard of him.”
Here’s the code:
output:
from climbMountain upOnMountain = just smell the cool, clean, air
from the valley upOnMountain = undefined
Actually, Mr. upOnMountain vanishes once you walk off the mountain. That’s because variables created inside a function - local variables - only exist for the duration of the function. When the function is over the variables are erased from existence.
The nice thing about the mountain and valley analogy is that it translates nicely if you indent your code - the way you’re supposed to. If you think of indenting as going up on the mountain then you can easily see where your variables scope exists.
I have another analogy to toss on you, and you’ll really like this one. You know the saying, “What happens in Vegas, stays in Vegas.” Well, it’s the same with functions. Suppose you and the people you know are like variables. While you are in Vegas you can certainly call home and talk to your significant other. You can do that because the scope of your relationships at home (_root) extend to Vegas.However, if you make a friend in Vegas then the scope of that friendship exists only in Vegas. Once you leave Vegas you cannot call them and doing so would result in an error.
(Note to wife: I swear this is a totally hypothetical analogy.)
Tools to help you find your way
The first and easiest tool is good old trace(). (Note: a shortcut to making a trace statement is ESC + tr.) With Flash it’s easy to bury code god-knows-where on timelines. Sometimes you loose your way and when you do the easiest thing to do is trace(this).
Okay, so what is this?
this is a reference to the current object.
Okay, what does that mean?
If you didn’t know, everything in Flash is an object. Every MovieClip, TextField, Number, everything. So this is a reference to the object where the trace() statement is made. (Note: I almost always use something like, trace(”something: ” + this), so that if I have multiple trace() statements I’ll be able to tell their outputs apart.)
Tracing this really comes in handy when you’re dynamically creating MovieClips and loading things at runtime and you loose track of where things are going.
If you’re MovieClips are placed on the stage at authoring time then by all means use the insert target path tool in the Actions Panel. The MovieExplorer Panel can also be a big help in helping you find assets.
If you want to see an output of all the graphical objects - MovieClips, TextFields, Buttons, etc - then publish the movie and hit Ctrl - L. That will output everything to the Output Panel. It’s a little messy to read if there are a lot of assets but you can always use Find to search for the instance name you are looking for. From there you can see what the path for that object is from the _root.
Lastly, I find it very useful when I’m dynamically creating MovieClips and TextFields to document what I’m creating. I use indenting to indicate parent/child relationships and don’t forget to add the depth which the asset was created at. If you’re dealing with really large projects it’s handy to do the same thing on a white board for quick viewing.