Use of eval in Flash
Sometimes I find programming Flash so frustrating. It seems to break convention in a number of areas that it often leaves me banging my head against my desk for hours. My recent beef: the use of "eval". In programming with javascript, I've enjoyed being able to use eval to evaluate a full expression. For example, eval("myimg.height + 20") would determine the height of my image and then add 20 to it. In Flash MX, the eval function can't evaluate an expression or a number. A couple examples: eval("Stage.height") will work but eval("Stage.height + 20") will not. Even eval("20") gives undefined. I'm hoping this has changed in MX 2004 or I'm going to have a very sore head!
Conversation
I have been experiencing a similiar behavior in 2004. Somtimes the eval() works other time it does not. In 2004 they recommend using the _root[] notation but I am having similiar results. If anyone has a consistent working solution to evaluating a full expression I would love to know it
Same goes for me, eval function sometimes works sometimes does not in professional 2004. Does anyone have any other solution ... I thought I could depend on this function .. I thought wrong.
I'm having troubles with eval and this also. I've tried using actual examples out of the docs and they don't work either. I think there is something seriously wrong with MX 2004. It's very frustrating; I can't think of a cludge.
Hey guys, I think I've figured it out.
I tried a few tests. The first thing I did was this:
var TextField1;
this["TextField1"] = 12;
trace ("TextField1 = " + TextField1);
TextField1 traced to equallying to 12. excellent. That's what I wanted. Then I tried this:
function testthis() {
var TextField1;
this["TextField1"] = 12;
trace ("TextField1 = " + TextField1);
}
testthis();
TextField1 traced to "undefined". Crap. That's no good. Then I tried this:
var TextField2;
function testthis2() {
this["TextField2"] = 12;
trace ("TextField2 = " + TextField2);
}
testthis2();
This worked! Note the variable declaration is OUTSIDE the function that I use the variable in? It seems as if that's want needs to happen. Hope that helps.
paSipHIsT: The problem you're having has to do with variable scope. This is definitely a topic that needs to be understood well to ensure that you're accessing variables or objects properly.
Jonathan:
I'm not sure what you mean to say. Are you telling me that the issue I was having (in the second example) was a scope problem (on my part)? I don't think so. I declare the variable in the function, before I use it in the same function. That should work.
The problem is in how you are referencing your variables and assigning them values. It's creating wierd behaviour related to scope.
The problem is that you are using
this
which allows you to reference an object within a particular context. If youtrace(this)
either in the root or in a function sitting on the root, you'll notice that it references_level0
. This is important when we look back at your code.In your second example you declare a variable inside the function. Then you try to use
this
to reference the variable but there's noTextField1
variable on_level0
, just the one in your function. So, when you go to trace out the value of your variable it returns undefined because you haven't assigned it a value yet.Well, sadly this is the expected behaviour. I quote below some lines of the Macromedia Flash MX Professional documentation:
"Some of the differences between ActionScript and JavaScript are described in the following list:
ActionScript does not support browser-specific objects such as Document, Window, and Anchor.
ActionScript does not completely support all the JavaScript built-in objects.
ActionScript does not support some JavaScript syntax constructs, such as statement labels.
In ActionScript, the eval() function can perform only variable references. " (...)
That is: You can use eval to obtain variables, but not to compute anything
http://www.adobe.com/cfusion/knowledgebase/index.cfm?id=tn_16187
Isn't it lovely :D
Jonathan,i tried to execute a ActionScript code as string format.
How to do it?
example:
var str="var t=0;t++;tarce(t);";
eval(str);//it's wrong!
Please tell me.thank you!
jsrpg: take a look at Mark Wubben's alternative. I haven't tried it myself but it might do the trick for you.
Ah,it isn't answer my question.it just return a value,but i need to execute the string-code.
The effect just like the eval() function in javascript.
_root["stage"]["height"] += 5;
If you want full (JavaScript type) functionality of the eval function in ActionScript 3, use the ExternalInterface class to call a javascript function in the html wrapper, thus:
Actionscript code:
Note that in my implementation, the javascript in the container html file is in its own <script...> block. Also, my implentation assumes AS3, and was tested in Flex 2.