PDA

View Full Version : good vs. EVAL



jasonoda
November 21st, 2005, 04:52 AM
sorry for the conry title

i am old school actionscripter who really liked using my eval(a+b)=10 style of scripting. apparently in flash 6 they did away with letting you do this for some reason. i learned the new way to do things which is like this:

sc=1
this["_root.e"+sc]=7
trace(this["_root.e"+sc])

but what annoys me is if i say this

trace(_root.e1)

i still get undefined and not "7"
is there a way that i can get the value of "e1" without having to write: trace(this["_root.e"+sc])

another example:
if i say:

sc=1
this["_root.e"+sc]=7
addit+=_root.e1
trace(addit)

addit just stays at 0

instead i have to say addit+=this["_root.e"+sc]

i think this is kind of dumb. one used to be able to do this back in the flash 5 days with eval. is there any other way to do this?

jason oda
starvingeyes.com

Mirandir
November 21st, 2005, 05:31 AM
The associative array can only handle one "level" in the dot-notation syntaxt at once.

This:


this["_root.e"+sc]=7


Should look like this:


_root["e" + sc] = 7;


/Mirandir

claudio
November 21st, 2005, 05:49 AM
Example 1:
When you use the array acess operator, the parameter on the left side of the brackets tell flash the scope that you're accessing. So if you wanted to set the variable in the _root, you should have used:
_root["e"+sc]=7;
What you did was actually set a variable named "_root.e1" in the current object.
But because _root is a keyword in actionscript, you could not retrive the value of that variable directly, instead you had to use the array access operator. The following example is kinda similar. Though you cannot set a variable named "!", if you use the array access operator you can trick flash.
_root["!"] = "hell yeah!";
trace(_root.!);//will give you a compiler error
trace(_root["!"]);//hell yeah!

[edit]Mirandir beat me :P

senocular
November 21st, 2005, 06:46 AM
there is also a thread in best of that covers some of this stuff
http://www.kirupa.com/forum/showthread.php?t=12082