my interests and experiences
RSS icon Email icon Home icon
  • AS3 – how to do eval() the right way

    In AS2 we used to be able to refer to an instance name of an object by coding:

    ///AS2.0\\\
    eval("my_mc"+i);

    This would allow you to create and/or refer to instance names using a string. I have heard quite a bit of negativity about eval() but it did come in handy at times. In AS2, you could also use:

    ///AS2.0\\\
    _root["my_mc"+i];

    which worked well for me when pushing items to an array, before AS3 was available.

    Now, the correct AS3 way to do it is either this:

    ///AS3.0\\\
    this["my_mc" + i];

    or:

    ///AS3.0\\\
    this.getChildByName("my_mc" + i);

    I would recommend the second way of doing it, because to me it just makes more sense.