PDA

View Full Version : Difference between eval () and _root[] ?



ilyaslamasse
March 19th, 2002, 05:45 PM
Well, the title says it all.

The thing is : I never got that eval () function to work. I've seen it quite a couple of times in fla though, but whenever I want to use it, it just won't work.

Thanks

pom 0]

upuaut8
March 20th, 2002, 03:10 AM
I'd tell you but I didn't bring that book with me. To tell the truth, I've only used it a couple of times as well, just because it seems to be tricky somehow. At least, I'm not doing something right with it either, but I've gotten it to work a couple times.

_root[] is another matter and I really don't know why you're comparing the two. Unless I'm misunderstanding what you're asking.

I would use it in a case like this.

for(i=0,i<200,i++){
_root["myVariable"+i]._x=something+i;
}

this is where I need to call to change the property of a series of movie clips with nearly identical names. since you can't use quotations inside a call function like that.. you have to use brackets to saround the information. The biggest mistake I make with this is forgetting wether the "." in front of the brackets is left out, or after them.

ilyaslamasse
March 20th, 2002, 04:48 AM
Well, I don't know, I thought that eval("thing"+i) was doing thingi, but obviously I'm mistaken...

pom 0]

eyezberg
March 20th, 2002, 01:39 PM
eval returns whats inside a variable, else you don't get the content, you get only the var's name!

thoriphes
March 20th, 2002, 04:02 PM
for (x = 0; x < 10; ++x) {
trace(eval("test"+x));
}
//output:

test0
test1
test2
test3
test4
test5
test6
test7
test8
test9

you also mentioned "_root[]". now you could achieve the same thing above without the "eval" function. but you can't simply do:
"test"+x;
you have to let flash know where this variable (or movieinstance name) is. that is where "_root[]" comes in. say all the variables were on the root. you can call them each like so:

for (x = 0; x < 10; ++x) {
trace(_root["test"+x]);
}

this would output the values in those 9 varibles.
now if the vars weren't in the root, like say, in a movieclip, you would call them like so:

for (x = 0; x < 10; ++x) {
trace(this["test"+x]);
}

notice in the previous two examples i didn't use eval, when you use bracket notation ("[ ]") you don't need it. eval is sorta like the same thing.
hope this helped.

upuaut8
March 20th, 2002, 05:43 PM
cool.. I never knew that.. :)

ilyaslamasse
March 20th, 2002, 06:44 PM
They don't seem to be interchangeable though. I mean, I don't think you can do :
movie = eval("copy"+i) ;

_root.movie._x = 50 ;Can you ?? I have to try that...

pom 0]

thoriphes
March 20th, 2002, 10:18 PM
actually, you can, i do it all the time. saves me a lot of typing.
eg:

mc = _root["copy"+x];
mc._x = random(550);
...

ilyaslamasse
March 21st, 2002, 01:13 AM
Dammit, it works...
OK, thanks Thor.

pom 0]