ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Paths to Objects and Variables
http://www.actionscript.org/resources/articles/17/1/Paths-to-Objects-and-Variables/Page1.html
Jesse Stratford
Jesse lives and works in Melbourne Australia. He is the Cofounder and a Director of http://ActionScript.org. A Flash enthusiast, teacher, author, freelancer and speaker Jesse enjoys participating in the http://ActionScript.org community and the wider Flash scene when he has time. 
By Jesse Stratford
Published on September 9, 2005
 
Written by: Jesse Stratford [email:jessestratford@actionscript.org]
Difficulty Level: Beginner
Requirements: Flash 5 or higher.
Topics Covered: Paths.

Page 1 of 2
Written by: Jesse Stratford [email:jessestratford@actionscript.org]
Difficulty Level: Beginner
Requirements: Flash 5 or higher.
Topics Covered: Paths.
There is no source file for this tutorial.

This tutorial deals with referencing objects in Flash via paths.

I decided to write this tutorial on day after answered what must be the millionth question which can be solved by investigating paths. This guy had a problem where his movie worked fine until he loaded it up inside another, on Level 1, at which point none of his buttons did as they were told and all his text fields died. In this tutorial we will discuss how Flash uses paths to keep track of objects and how we can use them to reference objects within our Flash files. While I will be talking about variables, you don't need to know any programming to complete this tutorial. This tutorial does not deal with animation along paths (or "Guides"), that is a completely different topic.

A path in Flash is analogous to a 'path' in a park; it's a way of getting from one place to another. A path in Flash directs Flash's interpreter to a specific object within the movie. Paths are vital to your understanding of Actionscript because ,without paths your entire SWF contents would have to reside in one place (on one level for instance), which is increasingly impossible with the fancy stuff designers and developers are doing these days.

Paths to objects.

An Object in Flash can be pretty much anything. Common Objects include MovieClips, Buttons and TextField but those of you who understand Object Oriented Programming will know that Objects in Flash are much more universal than these few examples. For this tutorial however, these few examples will suffice. While you're a beginner, paths are probably most applicable in terms of Buttons and Movie Clips, and using paths to reference more advanced Objects works just the same, so we will focus on these objects to begin with.

Imagine you have a movie clip on your main stage, called 'clip1' for example. Within this clip is another clip called 'clip2'. This hierarchical (level based) layout is great for games and navigation, and very common. Now imagine you have a button on the main stage also. When the button is clicked you want it to perform an action on 'clip2', (which action is not important, we'll use a visibility set in our example). Selecting your button you add the actions as follows:

[as]on (release) {
setProperty ("clip2", _visible, false);
// OR
clip2._visible = false;
}[/as]
Now you test your movie, hit your button and Wham!... Nothing happens.

Once you're over the initial shock you go back to your code and, scratching your head, wonder what you've done wrong. Thinking back to our example, recall that is on the main stage and it contains , (see picture, left).


Now, since contains , does not actually reside on the main timeline does it? No, it doesn't. It is on the timeline within the Movie Clip. We all know this, but Flash doesn't, so we have to tell it using paths in our syntax.

The table below shows the path syntax parameters and gives examples of how to use them. We go into more detail over the page so just take a look to begin with then we'll describe how it works.

Parameter Usage What it tells Flash
_level0 setProperty ("_level0"
"We're going right back to the lowest Level of the movie".

_root setProperty ("_root"
"We're going right to the bottom of the current Level; to the Main Stage on which our base Objects are stored."

When you get into loading separate SWF files into Levels, _root will refer to the base of the current level, while specifying a Level using the syntax shown above will allow you select a specific Level. If this is unclear to you, just ignore it. It will become clear if you ever load content into other Levels, which is becoming increasingly less common practice nowadays.

. (dot) setProperty ("_root.clip1.clip2"
"Whatever comes after this dot further defines the path to something." In this case, is on the timeline of which is on the _root.
But wait, there's more, turn the page and read on!

Page 2 of 2
I did my best to explain those but we really need to do a few examples to get a proper understanding. Let's consider our previous example:


  1. is on the main timeline.
  2. is within .
  3. The Main Timeline can be reference using "_root".
So, the path to is:
[as]_root.clip1.clip2[/as]
Note that Flash 4's "slash-syntax" for pathing is still supported but I no longer teach it in my tutorials as it's outdate. See the archived versions of the tutorials for Flash 4 pathing info. Also note that, as of Flash MX 2004, references to Objects are case sensitive, which means is not the same as .

While you're a beginner I always recommend using the full path to objects, this means using the "_root" at the beginning of every reference! This way you're much less likely to get silly but crippling debugging errors. Once you're confident you have the hang of pathing it's wise to drop full paths (known as 'absolute paths') and get into relative paths (see the Advanced Pathing tutorial) because they make your code much more extensible.

Now it's your turn. In the example below note the following:
  1. Oldest is within Middle.
  2. Middle is within Youngest
  3. Youngest is within/on the Main Stage

Finally, when loading movies within movies, using the LoadMovie command, people often encounter problems. This is often because their Objects which did exist on _level0 are now on _level1 or _level2 (depending on what Level you loaded to), or within a targeted Movie Clip. Remember that you can check paths to objects and variables in the Test Movie environment within Flash (Ctrl-Enter), by pressing Ctrl-Alt-V (variables) or Ctrl-L (Objects and Layers). If you need to reference an Object on another layer, it's as easy as: _levelX.objectName or _levelX.objectPath.objectName where X is the Level number.

Paths to Variables:

Variables are just a type of Object, so paths to variables (in Flash 5 and above) are specified in exactly the same manner as has been described above. For instance, to set the value of a variable called 'foo' within in our ongoing example from above, you would enter:
[as]_root.clip1.clip2.foo = "Some Value";[/as]
Paths to variables in Flash 4 were different; for more information see the archived version of this tutorial at the bottom of the Tutorials index.

That's it. I did my best on this one but it's hard to teach this stuff and assume no knowledge. I'm always open to suggestions.

Also note that there's now an Advanced Pathing tutorial (see Related Articles, below) but it's late in the Intermediate set because you're expected to read a few of the Intermediate tutes (like the arrays tutorial) before you try it. Good luck.

Jesse Stratford [email:jessestratford@actionscript.org] is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash.

NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there.

If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity.
This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it.