Advanced paths in ActionScript

Source: Internet
Author: User
Tags array copy function prototype variables relative
Senior Difficulty:Intermediate
Software Environment :Flash 5


Can you tell me how many tutorials you've read about my path? Well, a little bit. Is it not good to modularize what you have learned and to use what you learn anytime and anywhere? Of course, the manual input path is not an incisive script. So we need to go further today and learn the real path.

Dynamic path (array identification method)

Suppose you write an advanced script to copy several movie clip and place them randomly on the stage. Well done! Now I want you to browse through your scripts and make them lean half way. "I can do it," you say, writing code:

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

It's getting boring, isn't it? I know I will. Of course, we don't want to write a few codes for this tiring job. If you've read the loop tutorial, you'll know that this example is quite appropriate with a looping statement. The following code is written using a looping statement:

for (Var j=0 j<x; J + +) {_root.duplicate ... oh oh

Is it true? We all know you want to copy several times ... But how do you tell Flash that? OK, now you can use the dynamic path to solve. In Flash 4, we use the eval () function when we want to compute the result of an action or program. Today, we want to use square brackets ( [] ). You and I both know these parentheses: we use them when we use arrays. In fact, a dynamic path is a little different from accessing an object in an array. The idea is that we give flash a benchmark timeline and use it to look for elements that are grouped by one or more unknown elements (variables, mathematical results, and so on). In the example above, our variable J declares a copy of the current effect we want to apply. We also know that the names of those replicas are in the form of duplicate1 and Duplicate2. So we need to tell Flash to connect the number I to the string "duplicate". Don't worry, it's easy, let's take a look at the examples.

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

I think most of this code looks like you've seen it before. For loops you know, the last point symbol of the second line is familiar, but the first part of the second line is a little rusty, OK, I'll explain it now. In short, it tells Flash: "Notice the object on the _root timeline, whose name is composed of the string" duplicate "and the value I connected to afterwards. Note that a point is missing between the _root and the left parenthesis; this is not a syntax error, this is the correct dynamic path format. Also note that there is a point behind the right bracket that is the standard path you expect.

It's not bad! Examples of such things are always useful. Let's take a look at more content. We have an input box (variable input) that you want the movie clip entity name to appear in this box.

_root[input]._visible = false;

For the start stage, this example might be a little easier. The code is to simply tell Flash to compute the variable input, and then use this to create the path to the object. Of course, it's a bit difficult (but useful) here. Now you have a large number of movie clip entity names, and you want to set a random count for each _x.

_root.namearray = [Joe, Jenny, Jesse, Jumping-jude];for (var j=0;j<_root.namearray.length;j++) {    _ROOT[_ROOT.N amearray[j]]._x = random (200);}

It's obviously a bit difficult here, but it's a crash course, so you have to learn faster! It is not necessary to pay attention to this clear string of _root.namearry. You can simply use Namearry, but I would still like to keep my clear path for later debugging. Also note that you have to include _root in a dynamic path, and that flash requires at least the name of the timeline when you are pointing.

There are some complexities with array identification and dynamic path usage. Practice is the best way to learn, so you can give yourself some small topics to increase your experience! Once you're obsessed with it, let's move on.

Relative path

This chart was borrowed from my previous path tutorial. In the beginner tutorial, we discussed how paths can be associated with a tree structure. The trunk of the tree is the _root main timeline in which the movie clips (branch) is located, and each branch can hold other clip and objects (sub branches), each of which can contain variables (leaves). Let's say we're going to type some code into the CLIP2 to call a variable in clip1. The code is like this with the method that is taught in the introductory tutorial:

_root.clip1.variablename = value;

Great! But what if this SWF is loaded into another SWF? When loaded into a movie clip (we use Loadmovie ("file.swf", "TARGETMC"), all paths will be changed! The CLIP1 path is no longer _root.clip1 but _root.targetmc.clip1. It is true that we can examine and write the path carefully, but this means that our code cannot run without being loaded into another SWF. What we want is the kind of code that is not affected by the external environment and executes independently.

Relative paths can help us fulfill our wishes. Let's say we're on the stairs. I said, "Up level Three," you do it. I said, "go to the second stage." Would you go back to the bottom floor and start up the ground? No, you're lazy, you just move down one step. That's what we want Flash to do. We don't want it to go to the bottom of the path, because the situation is changing at any time, and we just want it to know what is relevant to it. Flash has some code structures that allow us to access the variable timeline associated with the current timeline. The two most important are this and _parent.

This refers to the currently active object or timeline. For example, if I open a new Flash file and add the following code to the first frame of the main timeline:

this.variable = value;

Flash creates a new _root-level variable and assigns a value. If I type the same code in a movie clip, the variable is created on the movie clip timeline. "And so what?" You say, "You can omit this only with variable names ." Not quite right. You can omit this, but Flash needs it. ActionScript is a very friendly language that allows you to be lazy or, to some extent, make a small mistake. If you omit the This command, the ActionScript compiler for Flash is automatically added at compile time to fit the code into the Flash player reading. In addition , this has a higher value for applications, especially for advanced programmers. It allows you to create your own function prototype and run the function on top of the object. It is also very useful in the with or telltarget commands. At this point, you just remember that it points to the current active timeline or object.

Now it's time to speak _parent.

At first, the use of_parent may be more pronounced. Look at the chart on the left and recall the similar steps. We know that Step 2 is step 3 to live in the front level of the stairs. So clip 1 for Clip 2, is to the upper level, that is to say Clip 2 is on Clip 1 of the time axis. So clip 1 is called the parent of clip 2. So, when we want to point to Clip 1 o'clock from Clip 2, we don't need to give all the paths starting from _root, but only relative paths:

_parent.variable = value;

Do you understand? The main time axis (_root) is the parent of clip 1. So if we're in Clip 1 and want to point to some element on the main timeline, we can use:

_parent.variable = value;

Of course, in this case we didn't write _root.variable to save the alphabet, but to use _parent.variable. Recall the Loadmovie example, if we use _root to load this swf into a movie clip, this notation looks as if the SWF being loaded is on _root. Using the _parent syntax, we can make sure that it looks like a variable on its parent timeline.

The _parent parameters can be superimposed. So when we're on clip 2 and want to jump two to the main timeline, we can use:

_parent._parent.variable = value;

The first _parent lets us jump from clip 2 to Clip 1, and the second lets us jump from clip 1 to the main timeline (again, in this case it can be _root, but in the Loadmovie example it might be completely different).

That's true. Children, to be a master, practice is the best way!



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.