AS: Hierarchy
Sit down class. Today I will teach you about Flash's built-in hierarchy. Yes, Ralph?
Ralph: "What is Flash, ms. Hoover?"
Shut up Ralph, that's none of your business. And stop eating boogers. What, Lisa?
Lisa: "Ms. Hoover, what is hierarchy?"
Hierarchy is a kind of structure, which uses words such as parent and child to describe relations between objects. In Flash, this structure is also used to store objects in a clean and ordered manner. This means you can structure your Flash work by keeping objects nested inside other objects.
When you place object A inside object B, A is a child of B, while B is the parent of A.
Ralph: "Ms. Hoover! Ms. Hoover! Is my booger my child?"
Shut up, Ralph.
Lisa: "Ms. Hoover, I can't see how this is related to Actionscript."
Then again you're just a snotnose kid, Lisa. You aren't even supposed to know what Actionscript is. Anyway, I was just about to get to that.
When you use AS, you need to be able to refer from one object to another, to make them interact. To do this, you have to know of certain parts in coding.
Names
Names are crucial when you need to keep track of objects. It's pretty simple really, if you don't know an object's name, how would you refer to it? If you place an object on the stage manually, there is a box in the Properties panel where you can enter the instance name of the object.
If you place an object on the stage via Actionscript, you will need to give it a name at the point of creation. You can also record it's target path to a variable, to keep a "shortcut" to it.
. (Dot syntax)
Using dot syntax is an essential part of Actionscripting.
A dot in the code is used to access a child of whatever is right before the dot. For example, if you write mc1. you have "entered" mc1, and can access all of it's children. In this example mc2 is a movie clip inside of mc1, therefore a child of mc1. This means that to access mc2, you have to write mc1.mc2. So, if you want to access the _x value of mc2, you have to write mc1.mc2._x. Get it?
_root
The root is the movie clip we often refer to as "main timeline", in other words the top-level movie clip which contains everything else. Call it Eve if you wish.
Eve: "Me?"
No, not you. You're in the wrong classroom, by the way.
If you open a new Flash document, draw something and convert it to a symbol, this symbol automatically becomes a child of the root movie clip. To refer to the root, use "_root." (Don't forget about the underscore). This is called an absolute path.
A negative thing about using _root is that it can have unwanted results when you load the .swf into another movie, because in that case the _root automatically gets moved to the parent .swf.
_root should be used if you are inside a child, grandchild, etc. of the root, and wish to access another child, grandchild, you know the drill, of the root. However, if you need to access another object that is not an heir of the object you are in, but in the same branch, that is, if you don't need to pass through the root to access it, you would rather use:
_parent
It's as simple as it sounds. It accesses the parent of the object you're on. See this picture for a visual explanation. _parent and this (see below) are used for so-called relative paths.
this
Yeah, that's right. This. The object on which you are. Many times, this is not needed though. For example, _x = 10 is just as good as this._x = 10. There are some times though, when this comes in handy. One reason to use this is simply to improve readability. Here's two more examples:
When you use the method function.call(), you have the parameter thisObject. It is the object that the word this in the function will refer to. Here you might want to use the object you're at, but excluding this would leave nothing, which does not suffice as a parameter.
This code on the main timeline will set the _root's _x to 100, since it doesn't use this:
ball.onEnterFrame = function () {
_x = 100;
};
While this would refer to the object "ball" inside the root:
ball.onEnterFrame = function () {
this._x = 100;
};
It should also be mentioned that to my knowledge, buttons can't be parents. Of course they can be a child of a movie clip, but just consider them impotent (ask your mother) when it comes to parenting. In fact, that's a bit of a lie, because they can contain movie clips and other buttons, but it's children can't be targeted with Actionscript. Let's just call them foster parents.
Arrays are an exception from the dot syntax. Read AS: Arrays by Denvish for more info on that.
Here are some targetting examples to practice on, just to see if you got the hang of it.
How would you refer to:
- mc2, a child of mc1, a child of the root, which is where you are?
- mc3, a child of mc2, a child of mc1, a child of the root, which is where you are?
- mc2, a parent of mc3, which is where you are?
- mc3, a child of mc2, a parent of mc4, which is where you are?
- mc5, a child of mc3, a child of mc1, a child of the root, a parent of mc2, a parent of mc4, which is where you are?
Oh well, that's all for today, kids. Have a nice... Oh, they're gone.
... Ralph? Are you still here? Since you are, I could always tip you about the Target Path button. It's right on top of the Actionscript panel, and you can use it if you are too stupid, or too lazy, too do what this lecture was all about. It was about hierarchy, Ralph. Stop picking your nose.
