Tutorial details:
Written by: Jesse Stratford
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:

_root.duplicate1._xscale = 50;
_root.duplicate1._yscale = 50;
_root.duplicate2._xscale = 50;
_root.duplicate2._yscale = 50;
_root.duplicate3._xscale ...

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:

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

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.

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

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...