PDA

View Full Version : _root question


cheez
12-21-2002, 01:48 AM
Howdy, so much to learn...

The expression below did not execute correctly until I added the "_root" suffix. Essentially the "label" name of my checkbox also shares the same name as one of my particular MCs. In general terms, can someone give a rule o' thumb at when to use (or at least consider) using _root.

Thanks,
Cheez

/////////////////////////////////////////

function onSelectA546em(checkbox) {
test1 = checkbox.getLabel();
_root[test1]._alpha = 100;}

/////////////////////////////////////////

emergency_pants
12-22-2002, 02:07 AM
It's all to do with where you are accessing your variables, your functions and your movieclips FROM, and more importantly what your TARGET is... and a matter of organisation.

Wherever possible, I tend to keep all my variables and functions in the root of my main movie. That way, I always know where they are and I don't have to bother using complicated paths. I guess you could then describe them as global.

I often prefix them with a "g" so I know they are global and then I also always know to access them from ANYWHERE in my movie simply by putting _root in front of the command. It's easier to track stuff and it makes the scripts cleaner and easier to track.

For example, I might want to make a game and keep a variable called "gScore". I could have a movieclip called "scoreMovie_mc" with a cool number animation in it. I COULD also keep my gScore variable in there. The thing is, every time I want to change the gScore by 20, I have to put:

_root.scoreMovie_mc.gScore += 20;

This is a bit irritating and makes my fingers ache! (lazy, eh?)

So I like to put the gScore in the _root of my main movie. That way, every time I change the gScore, I can say:

_root.gScore +=20;

I can use that line to increase the score from ANYWHERE in my movie... it could be a script inside several different Baddies, it could be a function, it might be a script inside a bullet movieclip, etc, etc. It doesn't matter how complex my equasions are, or how the hell I get to the result, as long as I remember that gScore is in the _root of my movie... that is my end target.

I guess it's a matter of preference on the whole, but that's the way I do things. I don't like it when I get into a situation where I am calling a function, which is inside one movieclip, which refers to a seperate movieclip, and uses a set of variables scattered across other movieclips. It just becomes a big web of pathnames.

The trick is to think about where you are accessing the variable/movieClip/function FROM and figure a path from your access point to it. If I were to try and access that gScore variable from within another movieclip, and I DON'T use the _root prefix, it won't be found because the script will only look in the current movieclip (the accesspoint) so you have to tell the script WHERE the variable is, or where the movieclip is, or where the function is.

It looks like your script's final aim is to change the _alpha value of a movieclip, which lives in the _root of your main movie. That is your final target, and that is why you probably needed to put _root there. Your starting point was somewhere in your movie that WASN'T in the _root of your main movie... so Flash needed to be told where that movie is.

Well.. I've confused myself now, so I'll stop... but I hope it makes sense.

Simon.

pom
12-22-2002, 02:12 AM
Scoping your variables with _root can be dangerous when you load your swf into target though. Be careful.

pom :)

emergency_pants
12-22-2002, 02:17 AM
heehee... yeah, I've done that before! :)

Mortimer Jazz
12-22-2002, 12:39 PM
It is not that you need to use _root here, it is that you need to evaluate the value you have retrieved from the component.
That may sound confusing so I'll try my best to explain.

You could have used:
eval(test1)._alpha = 10;//old way of doing things
or
this[test1]._alpha = 10;//new way to evaluate
instead.

The value you have assigned to test1 is a string (you can check this by placing this trace in your code: trace(typeof test1);)

You want to change the _alpha of the object which has the same name as your label. To do this you need to evalute your string to a path.

It just happens that your object is on the root, and as your code is also on the root it is not strictly necessary to specify a path (because they are in the same location).
However, with the new method of evaluating you can't just say [objectName]. With the old method (using "eval" you could).
So, this gives you the option of either using the 'this' keyword or an absolute path "_root"

Hope this helps

cheez
12-22-2002, 09:52 PM
First:

Damn, I like this forum.

Second:

Thanks everyone for the comprehensive answers :)

OK, so indeed this variable does lie in the maintimeline (which I assume is _root) as opposed to the symbol-mc. Actually, at the expense of noobing-out, if I had a symbol1-mc inside a symbol2-mc which is then inside the maintimeline, would symbol1-mc need a specific path to locate it?

Other that, I think I understand the rest of your reply. "this" refers to whatever timeline you happen to be in at the moment. It just so happens that I was in _root. Thus it can be called either way, however, it DOES need to be specified because it is an object. The string is "test1" however, when it is used in a call to ._alpha, I'm guessing by definition, it must be an object. And it is. The MC "test1" is very different from a string called "test1".

Wow, am I still talking outloud?

Thanks again,
Cheez 8)

Originally posted by Mortimer Jazz
It is not that you need to use _root here, it is that you need to evaluate the value you have retrieved from the component.
That may sound confusing so I'll try my best to explain.

You could have used:
eval(test1)._alpha = 10;//old way of doing things
or
this[test1]._alpha = 10;//new way to evaluate
instead.

The value you have assigned to test1 is a string (you can check this by placing this trace in your code: trace(typeof test1);)

You want to change the _alpha of the object which has the same name as your label. To do this you need to evalute your string to a path.

It just happens that your object is on the root, and as your code is also on the root it is not strictly necessary to specify a path (because they are in the same location).
However, with the new method of evaluating you can't just say [objectName]. With the old method (using "eval" you could).
So, this gives you the option of either using the 'this' keyword or an absolute path "_root"

Hope this helps

Mortimer Jazz
12-23-2002, 10:26 AM
yep, sounds like you've pretty much got it sussed. In your other scenario you would need to evaluate using a full (or relative) path to the object in question .

Something that confused me initially (because it was badly explained to me when I was new to AS) was assigning var names. For instance you will still find yourself doing things like this:
myMc.duplicateMovieClip("name" + i, i);
Here you don't need to evaluate because you are just assigning an instance name to your duplicated object. Flash is intelligent enough to realise that you can't add a string and a number, so it turns the number to a string and concatenates the two to form something like "name0" or "name1". Of course, if you want to change the properties of one of those duplicated movieclips you need to evaluate that string to an object :)