Tip:
Highlight text to annotate it
X
Now let's take a look at the keywords this and root.
If you are following along I'm working in 06_this_and_root.fla in the Chapter 02 folder
or you can just use the same file from the last exercise.
Let's go to the first keyframe in the Actions layer
and open up the Actions panel. Here I have emptied out the moo function.
Now we will take a look at the value of this versus _root.
As I mentioned earlier _root represents the main Timeline. So inside of the moo function
if I trace _root and click on the cow, if I tested the movie
then I get _level0 in Output window. _level0 represents the main Timeline.
Essentially levels are depths. For more information about levels,
you can look at Flash Help. So now let's say we trace(this)
inside of the function. Let's see what we get.
We get _level0.cow_mc so it's a reference to the cow.
Because this function is attached to cow_mc through its onRelease property,
this is referring to cow_mc. So if were to cut and paste this trace statement
outside of this function then I would get the main Timeline. So we
are going to press Cmd+X on the Mac or Ctrl+X on the PC to cut,
Cmd+V on the Mac, Ctrl+V on the PC to paste. So now when I test the movie,
in the Output window I get _level0. So you can see that this is relative but depending
on its context. In a function that's attached to an Object
like our eventhandler here, this represents the Object that the function
is attached to and outside of functions and methods,
this represents the object where the code resides,
in this case the main Timeline. Now if I were to put this trace statement
inside of cow_mc then this would represent that instance of
cow_mc. So that's how this and _root work in ActionScript
2.0.