ActionScript.org Flash, Flex and ActionScript Resources - http://www.actionscript.org/resources
Advanced Pathing
http://www.actionscript.org/resources/articles/113/1/Advanced-Pathing/Page1.html
Jesse Stratford
Jesse lives and works in Melbourne Australia. He is the Cofounder of http://ActionScript.org. A Flash enthusiast, teacher, author, freelancer and speaker Jesse's main focus nowadays is managing http://ActionScript.org, but he enjoys participating actively in community and the wider Flash scene when he has time. 
By Jesse Stratford
Published on September 9, 2005
 
Tutorial details:
Written by: Jesse Stratford [email:jessestratford@actionscript.org]
Time: 40 minutes
Difficulty Level: Intermediate
Requirements: Flash 5 or higher.
Topics Covered: Relative and dynamic paths to ActionScript objects.
Assumed knowledge: Variables, loops, arrays, paths, instances.

Why you need dynamic paths
Tutorial details:
Written by: Jesse Stratford [email:jessestratford@actionscript.org]
Time: 40 minutes
Difficulty Level: Intermediate
Requirements: Flash 5 and above.
Topics Covered: Relative and dynamic paths.
Assumed knowledge: Variables, Loops, arrays, Paths, Instances.

So how many of you read my paths tutorial? Hmm a few. It's good being able to modularize your content and access it from anywhere isn't it? But of course manually entering paths isn't the way of the elite Intermediate scripter. So today we're going to learn about real paths, no more basics.

Dynamic Paths (Array Notation Method).

So (let's pretend) you've made some high-tech script which duplicates x copies of a movie clip and places them in random positions on the stage. Good for you. Now I want you to go through and make them all half as big. "I can do that" you say, and off you go:

[as]_root.duplicate1._xscale = 50;
_root.duplicate1._yscale = 50;
_root.duplicate2._xscale = 50;
_root.duplicate2._yscale = 50;
_root.duplicate3._xscale ...
[/as]

Bored already? I know I am. Of course we don't want to write 10 lines of code to do this menial task. Those of you who read the loops tutorial will know that such a task is a perfect application of scripted loops. So whack in a loop and it becomes:

[as]for (var j=0; j<10; j++) {
_root.duplicate ... oh oh
}
[/as]

Oh oh indeed! We all know you want to effect duplicateX now... but how do we tell Flash that? Well, this is where dynamic paths come in. In Flash 4 we used the eval() function when we wanted to evaluate the result of an action or process, not take it literally. Nowadays we use the square braces ( [ ] ). Of course you all recognize those braces: we use them with arrays all the time. As a matter of fact, dynamic pathing isn't so different to accessing objects in an array. The idea is we give Flash a base timeline upon which to look for an element with a name constructed from one or more unknowns (variables, results from math operations, etc.). In the case above, we have a variable j which indicates the current duplicate which we wish to have an effect upon. We also know that the duplicates are named in the form duplicate1, duplicate2, etc. So we need to tell Flash to concatenate the number j onto the common string base "duplicate". Don't worry, it gets easier, let's look at an example.

[as]for (var j = 0; j<10; j++) {
_root["duplicate"+j]._xscale = 50;
}
[/as]

Doubtless most of you have seen something like this before. The for loop you know about. The dot notation on the end of Line 2 is familiar also, but the first bit of Line 2 is a bit new, so let's examine it. In a nutshell it tells Flash: "Look on the _root timeline for an object whose name is composed of the string "duplicate" with the value of j concatenated onto the end". Note the lack of a dot between the _root and the opening square brace; that's not a syntax error, it's the correct form for dynamic pathing. Note also that there is a dot after the closing brace, just as you would expect in a standard path.

Turn the page...

Examples and relative paths
So, that's not so bad is it? Examples are always good for this sort of thing so let's look at a few more. You have an input field (variable input) and you want to make the movieclip with the instance name entered into the field disappear.
[as]_root[input]._visible = false;[/as]

This might have been an easier example to start with. It's as simple as telling flash to evaluate the variable input then substitute it in to create a path to an object. Then of course there's the more difficult (but useful) stuff. Say you have an array of movieclip instance names and you want to set the _x of each to a random number.

[as]_root.nameArray = ['Joe','Jenny','Jesse','Jumping-Jude'];

for (var j=0;j<_root.nameArray.length;j++) {
_root[_root.nameArray[j]]._x = random(200);
}
[/as]

That's a bit more complicated obviously but this is a crash course so you have to learn fast! Also note that specifically stating _root.nameArray each time isn't necessary; you could simple use nameArray, but I like to keep my paths specific for debugging. Note also that you must include the _root in the dynamic path; Flash needs at least the name of the timeline to look on.

In terms of array notation and dynamic paths, that's about as complex as it gets. You'll learn this best by experience so set yourself a few tasks to do and experiment! Once you're happy with that, we'll move on.

Relative Paths.

Tree ExampleThis diagram has been stolen from my previous tute on paths. In the Beginner level tutorial we discussed how paths can be associated with a tree structure. The 'trunk' of the tree is _root on which we have movieclips (branches), each of which can contain other clips and objects (sub-branches), each of which can contain variables (leaves). Say we're typing up some code within Clip2 which will call a variable in Clip1. The method taught in the beginner tute would be to use code like this:

[as]_root.clip1.variableName = value;
[/as]

And that's fine. But what if this SWF is going to be loaded into another SWF post-publishing? When loaded into a movieclip (say we use loadMovie("file.swf","targetMC");) all the paths will change! Clip1 is no longer _root.clip1 but _root.targetMC.clip1. Sure we could go through and rewrite all our paths but that would mean that when not loaded into another SWF, our code wouldn't function. What we're looking for is portability such that regardless of external influences, our code will hop along doing it's thing, oblivious to what's going on around it.

Relative pathing allows us to achieve this. Say we're on a staircase. I say "Walk up 3 steps" and you do. Now I say "Go to step 2". Do you come back down to the bottom and walk up again from step 0? No, you're lazy and you just step back one. That's what we want Flash to do. We don't want it to go all the way to the bottom of the path, because that might change from time to time, we just want it to know where things are in relation to itself. Flash has a few built in bits of code which allow us to access the various timelines relative to the current timeline. The two most important are this and _parent.

this makes reference to the current active object/timeline. For example, if I open up a new Flash file, and add the following code to frame 1 of the main timeline:

[as]this.variable = value;
[/as]

Flash will produce a new _root level variable with a value. If I am within a movieclip and I type the same code, the variable will be created on the movieclip timeline. "So what?" you say, "You can do that just with the variable name and leave out the this bit". Not quite true. You may leave out the this bit, but Flash needs it. ActionScript is a very user-friendly language: it allows for human laziness and error to some extent. When you leave out the this command, Flash's ActionScript interpreter automatically inserts it at compile time, to make it easier for the Flash player to read. Further, this has some invaluable applications, especially for higher level scripters. It allows you to make your own function prototypes and run functions over objects. It's also useful in with or tellTarget command blocks. For the moment, just remember that it makes reference to the active timeline/object.

Now onto _parent. Turn the page...

More relative paths
Tree Example_parent is perhaps more obviously useful at first. Take another look at the diagram to the left and recall the steps analogy. We know that step 2 is one step back from step 3. We know that Clip1 is one step up (back) from Clip2; that is, Clip2 is on Clip1's timeline. Clip1 is therefore called Clip2's parent. So when we want to make reference to Clip1 from Clip2 we needn't give the full path from _root. Instead we use the relative path:
[as]_parent.variable = value;[/as]

Got it? The Main Timeline (_root) is the parent of Clip1. So if we're in Clip1 and we want to make reference to something on the main timeline we can use:

[as]_parent.variable = value;[/as]

Of course, in this case we save ourselves no typing as _root.variable is less characters than _parent.variable, but recall the loadMovie example. Were we to load this SWF into a movieclip with the _root reference, it would look on the _root of the SWF that loaded it. With the _parent syntax we ensure it just looks to its parent timeline for the value.

The _parent parameter is stackable also. So if we're on the timeline of Clip2 and we want to go 'up' two levels to the Main Timeline, we can use:

[as]_parent._parent.variable = value;[/as]

The first _parent takes us from Clip2 to Clip1, the second from Clip1 to the Main Timeline (which in this example is _root, but again, in the loadMovie example may be something completely different).

So that's it really. Experimentation is the name of the game now boys and girls. Practice is the best way to become familiar with this stuff. Soon it will be second nature. 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.