00:00
00:00
Newgrounds Background Image Theme

Mekatov just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

AS: Hierarchy.

5,451 Views | 27 Replies
New Topic Respond to this Topic

AS: Hierarchy. 2005-11-23 17:37:06


AS: Main

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.


BBS Signature

Response to AS: Hierarchy. 2005-11-23 17:44:13


Very nice, and some humor to learning actinoscript.

And now i can link this to n00bs.

Didn't learn anything new but very throughly explained.

Nice one.

JPI


SIG YOINK!

BBS Signature

Response to AS: Hierarchy. 2005-11-23 17:46:01


Thank you, Ralph.


BBS Signature

Response to AS: Hierarchy. 2005-11-23 19:08:22


Lol, I laughed, I cried, I learned =-P

Response to AS: Hierarchy. 2005-11-23 19:13:54


At 11/23/05 07:08 PM, Drake_SI wrote: Lol, I laughed, I cried, I learned =-P

Then I have fulfilled my purpose on this forum =D

Thankyousir.


BBS Signature

Response to AS: Hierarchy. 2005-11-23 19:25:02


Don't copy names from the Simpsons please :(


wat

Response to AS: Hierarchy. 2005-11-23 19:28:46


At 11/23/05 07:25 PM, -Thomas- wrote: Don't copy names from the Simpsons please :(

Hey, it's just a gag, no seriousness. The "humour" was just a side-kick to keep you interested in the somewhat boring nag about hierarchy.


BBS Signature

Response to AS: Hierarchy. 2005-11-23 19:30:49


At 11/23/05 07:08 PM, Drake_SI wrote: Lol, I laughed, I cried, I learned =-P

Haha. Me too!

ditto.

Very neat tut, Rantzien!

Response to AS: Hierarchy. 2005-11-23 19:35:38


At 11/23/05 07:30 PM, Darkfire_Blaze wrote: Very neat tut, Rantzien!

Thank you. =)


BBS Signature

Response to AS: Hierarchy. 2005-11-23 21:14:25


nice tut for noobs lol and nice "humor" lol. something made me...lol


*User Page under construction*

BBS Signature

Response to AS: Hierarchy. 2005-11-23 22:40:17


At 11/23/05 05:37 PM, Rantzien wrote: . (Dot syntax)

great tutorial rantzien, but i would like to expand a bit on this:

[] - Array syntax
often, youll want to reference a dynamic object (one in which the name is called by a variable). let us pretend, ralph, that youve lined three of your boogers up on the table, and lovingly named them "booger1", "booger2", and "booger3", respectively. now, if someone were to ask you "what is an easy way to call all of these boogers?", you could say:

for (i=1; i<=3; i++){
_root["table"]["booger"+i].doAction();
}

the syntax looks similar to an array because you can consider a stage set up as an array. the _root houses many objects, all stored in the _root "array". one of these objects (objecta), may be parent to 3 children objects, stored in the _root["objecta"] array.

one final good use for this is the for...in loops, where you reference a parent object, and it will return all objects in that array. the following code will find all of the snotnosed kids in ms hoovers class, and make them behave:

for (i in _root["hooversclass"]){
_root["hooverclass"][i].behave();
}


BBS Signature

Response to AS: Hierarchy. 2005-11-24 01:12:36


Excellent Advice. You should make more...

Response to AS: Hierarchy. 2005-11-24 01:32:16


At 11/23/05 09:14 PM, JD77 wrote: nice tut for noobs lol and nice "humor" lol. something made me...lol

Thanks

At 11/23/05 10:40 PM, authorblues wrote: great tutorial rantzien, but i would like to expand a bit on this:
[] - Array syntax

Thank you for that addition. I knew I had forgot something.

At 11/24/05 01:12 AM, Guy_Fawkes wrote: Excellent Advice. You should make more...

Thankyou


BBS Signature

Response to AS: Hierarchy. 2005-11-24 01:41:31


At 11/24/05 01:32 AM, Rantzien wrote: Thank you for that addition. I knew I had forgot something.

you didnt forget it. you just brushed it off with a redirect to AS: Arrays. unfortunately, denvish didnt exactly cover how the timeline sets itself up as an array, so i figured id answer the question. overall, your tutorial covers the most important thing for a new programmer to learn.

writing these AS threads, we often take for granted how far we have already come. hierarchal setup is something that makes sense to us, but so often, these simple things are hard for up-and-coming programmers to grasp, and as far as the bare-bones of programming, this is one of the first things to learn.

and for doing the dirty work and actually typing this up, i commend you.


BBS Signature

Response to AS: Hierarchy. 2005-11-24 01:52:00


At 11/24/05 01:41 AM, authorblues wrote:
At 11/24/05 01:32 AM, Rantzien wrote: Thank you for that addition. I knew I had forgot something.
you didnt forget it. you just brushed it off with a redirect to AS: Arrays.

Actually I did forget it, I was just thinking about simple array access when I wrote that =)

unfortunately, denvish didnt exactly cover how the timeline sets itself up as an array, so i figured id answer the question. overall, your tutorial covers the most important thing for a new programmer to learn.

Yeah, the array access isn't crucial for learning hierarchy, but when it comes to targetting, which this tutorial was blended a bit of, then it can come in pretty handy.

writing these AS threads, we often take for granted how far we have already come. hierarchal setup is something that makes sense to us, but so often, these simple things are hard for up-and-coming programmers to grasp, and as far as the bare-bones of programming, this is one of the first things to learn.

and for doing the dirty work and actually typing this up, i commend you.

Yeah, thanks.

Gotta go


BBS Signature

Response to AS: Hierarchy. 2005-11-24 02:06:58


At 11/23/05 09:14 PM, JD77 wrote: lol

lol


BBS Signature

Response to AS: Hierarchy. 2005-11-24 05:31:35


At 11/24/05 01:52 AM, Rantzien wrote:
At 11/24/05 01:41 AM, authorblues wrote: writing these AS threads, we often take for granted how far we have already come. hierarchal setup is something that makes sense to us, but so often, these simple things are hard for up-and-coming programmers to grasp, and as far as the bare-bones of programming, this is one of the first things to learn.

and for doing the dirty work and actually typing this up, i commend you.
Yeah, thanks.

Gotta go

What I meant to type here before I realized my bus was about to leave (which I by the way missed =( ), was that when I finished this tutorial I browsed around to see if I forgot anything, and I couldn't find one half-decent tutorial about it! The best I could find was in the Help document of Flash, and that wasn't really a tutorial on it either.

At 11/24/05 02:06 AM, GuyWithHisComp wrote:
At 11/23/05 09:14 PM, JD77 wrote: lol
lol

Stop lolling in my thread =D
Oh, and happy birthday! *Hands over cake* I drooled a bit on it, hope that's ok.


BBS Signature

Response to AS: Hierarchy. 2006-01-07 13:37:42


Maybe you could do a little about _global

for all the really sad people out there :D

Response to AS: Hierarchy. 2006-01-07 13:58:45


At 1/7/06 01:37 PM, T-H wrote: Maybe you could do a little about _global

for all the really sad people out there :D

That's not a bad idea, maybe I will.

Some day.

BBS Signature

Response to AS: Hierarchy. 2006-01-07 14:05:14


At 1/7/06 01:58 PM, Rantzien wrote: That's not a bad idea, maybe I will.
Some day.

Lazy ass mo' fucker.

_global is an identifier, it makes the preceeding instance a global variable/class/etc which basically means it can be used anywhere in the Flash. For example, I have a class that adds the variables "mouseHeld" (when the mouse is held down), "mouseX"/"mouseY" (the AS3.0 names for _xmouse/_ymouse), "mouseSpeed" (mouse speed) and "mousePosition" (an array with the mouse _x and _y). Each of these variables is accessible anywhere in the Flash, not just where they are defined (where a new instance of the Class is created). The class is:

dynamic class MouseControl extends Mouse {
public function MouseControl() {
var mouseListObj:Object = new Object();
mouseListObj.onMouseDown = function() {
_global.mouseHeld = true;
};
mouseListObj.onMouseUp = function() {
_global.mouseHeld = false;
};
mouseListObj.onMouseMove = function() {
_global.mouseX = _root._xmouse;
_global.mouseY = _root._ymouse;
var curMousePos:Array = [mouseX-mousePosition[0], mouseY-mousePosition[1]];
var curMouseMath:Number = (curMousePos[0]*curMousePos[0])+(curMouseP
os[1]*curMousePos[1]);
_global.mouseSpeed = Math.sqrt(curMouseMath);
_global.mousePosition = [mouseX, mouseY];
};
Mouse.addListener(mouseListObj);
}
}

and yeah, I use it all the time.

That's basically it - official documentation here.


Sup, bitches :)

BBS Signature

Response to AS: Hierarchy. 2006-01-07 15:09:45


At 1/7/06 02:05 PM, -liam- wrote: Lazy ass mo' fucker.

No objection =P

description

Thanks =)

class

That's a useful class. I think I will make one too. =)
Gonna watch Gangs of New York now


BBS Signature

Response to AS: Hierarchy. 2006-01-07 15:12:33


At 1/7/06 03:09 PM, Rantzien wrote: Gonna watch Gangs of New York now

Good film. too long though : \

Response to AS: Hierarchy. 2006-01-07 15:28:21


At 1/7/06 03:12 PM, TimHeasman wrote:
At 1/7/06 03:09 PM, Rantzien wrote: Gonna watch Gangs of New York now
Good film. too long though : \

=)

Is that a permanent name change?


BBS Signature

Response to AS: Hierarchy. 2006-01-07 15:32:30


At 1/7/06 03:28 PM, Rantzien wrote: =)

Is that a permanent name change?

I've been meaning to give it a try for a while. figured I would tonight because I'm sick of people calling me T-H, just call me Tim : \

So far, I don't really like the look of it though, so we'll see :D

Response to AS: Hierarchy. 2007-05-09 09:45:25


Really good read, I actually laughed. XD I never really understood the use of _parent until now. Thanks!

Response to AS: Hierarchy. 2007-05-25 02:26:10


At 11/23/05 07:08 PM, Drake-SI wrote: Lol, I laughed, I cried, I learned =-P

That pretty much says it all. Thank you so much, this is helping me out a lot!


AS: Main

Don't read this sentence.

Response to AS: Hierarchy. 2007-08-21 06:34:13


heyy thanks heaps for that i learnt heeaps!!!
i learned what a parent was and how it works and also what this. means!!:):):):):)
cheers


account moved, delete this account tom...please lol

Response to AS: Hierarchy. 2008-07-27 03:04:58


At 8/21/07 06:34 AM, rickyboyce wrote: heyy thanks heaps for that i learnt heeaps!!!
i learned what a parent was and how it works and also what this. means!!:):):):):)
cheers

You didn't know what a parent was?

Are you a Test-Tube baby?