eval() is gone is Actionscript 3

I was trying to refer to a movieclip object whose name is computed from a variable. In AS2, I would be using something like eval(variableName).property to read/write to the property of the movieclip. I found that it no longer work in AS3 as eval() has been removed. After some research through the AS3 reference, I found out there is a new method call getChildByName()  under the DisplayObjectContainer class. It goes something like this:

 getChildByName(variableName).x = 100;

The above would set the property of a movieclip with the name stored by the variable variableName.

So when one door is closed, another door would open. 🙂

Advertisements

~ by brianchau on December 23, 2007.

23 Responses to “eval() is gone is Actionscript 3”

  1. I never needed eval(). In AS3 someparent[variableString].x still works. Do more, type less 🙂

  2. thanks a lot, brian
    this issue was making me crazy!
    you’re great!

  3. hello
    I want to ask if I want to make dynamik function that creates several movieClips and giv every one an inctance mane depends on the for loop for example?while attachMovie method is removed

    thank you 🙂

  4. suppose i have 3 swf loaded inside each other in “main_mc” main movieclip.. without Eval how i’m going to access this combination main_mc.bg_mc.one_mc._txt.text=”Hello”;

  5. I have problem how do I use a variable to create an object i.e.

    var mymov = new this[“myov1”];

    does not work

  6. If you want to create an object instance typed Object, the code is:

    var myObj:Object = new Object();

  7. eval was very useful for evaluating paths to variables and functions in one simple call. Suppose a system executes a series of command strings, one of which may look like this: “submitonly|LxQx|_level2.custominput”. In ActionScript 2, I could eval the last parameter to convert the path (_level2.custominput) into an object reference, but now that’s going to require me to parse the string by splitting it on periods and looking up each part with bracket notation starting from the stage or the root like this: {obj = root; do {obj = obj[part];part=nextpart;} while (moreparts && obj != null); return obj;}

    While on the subject of evaluating paths, I’m glad _lockroot is gone too (right?). We can now, I think, use “stage” to refer to the true root, and “root” to refer to the loaded .swf’s root if more than one is loaded. Also, I suppose it’s good the “_levelx” notation is gone, since I can just create my own empty movie clips to achieve the same layering effect. The built-in functionality with simple methods for loading and unloading clips into levels was nice though… that’s part of what made Flash take off to begin with. I like that Adobe is cleaning up Flash, but I just hope they don’t strip it down so elegantly that there’s nothing left and we’re writing our own parsers too.

  8. I have a problem that has been driving me nuts. Would anyone know how to load a variable from a combination of strings? For example, I need to load a ResultEvent called “evt.result.response.widgets.row[i].options” but I “options” comes from a string. So I need to load “evt.result.response.widgets.row[i].”+ “options” ? Anyone?

  9. You should be able to do that using array notation if you want dynamic paths:
    evt.result.response.widgets.row[i][“options”]
    evt[“result”].response.widgets.row[i][“options”]
    evt[“result”][“response”].widgets.row[i][“options”]
    And so on…

  10. I have three buttons on a form created dynamically and can reference the selected one on click through the currentTarget of the CLICK event handler. This works fine.

    Does anyone know how can I reference the other two buttons that aren’t clicked? Each button has a NAME associated with it and I have tried variants of getChildByName but haven’t hit the right combination of syntax yet.

    I need this as I want to animate the non-clicked buttons if you see what I mean

  11. You shouldn’t need to use eval – period. The use of eval and dynamic paths may mean that you are not designing your architecture using the standard approach of computer science. I suggest taking a look at Design patterns and purchasing a book or three on this topic.

    http://en.wikipedia.org/wiki/Design_pattern_(computer_science)

  12. hello,

    we can say I need eval for string that will return true or false…
    we have a function isCorrectAnswer(Q:string,A:string):boolean…

    so

    I need someThing like :

    eval(“(isCorrectAnswer(‘myQ1′,’myAn1’) && isCorrectAnswer(‘myQ2′,’myAn2’)) || isCorrectAnswer(‘myQ3′,’myAn3’)”);

    anyIdea how to resolve this?

    thks In advance,

  13. anybody know how to do this? I have 4 different coordinate variables set called x1,x2,x3,x4. I then have a function which generates a random number between 1 and 4. I want to take that generated number and add an x to it, so that it becomes, for example, x1. Then I want it to take on x1’s value, for example: 95. How would I do this?

  14. How would I get the result of an E4X expression, that I type into a TextInput, to a Text component from an XML object?

  15. I think you should try D.eval, it’s really cool

    http://www.riaone.com/products/deval/index.html

  16. YOU LEGEND!!!! Ive been looking for this for ages!!!

  17. thaks so much. i spent my last 30 minutes for to learn this 🙂

    and i finally came here thanks to google 🙂

    and of course thanks so much to you 😉

  18. i would like to know how to convert a string to an object??.And also i would like to add properties to that object..

  19. thanks brianchau

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: