javascripteaseljsanimate-cc

Animate CC - Call instance with var Strings


I'm trying to make a function that make play a symbol animation (hijo) when i click other one (padre). I want a function to use them many times so I'm trying to automatized.

Im getting the instance name of click button "padre" and try to add some sting after that in this case "hijo". I created some vars but when i use string value it doesn't work.

this.padre.addEventListener("click", nose.bind(this));

function nose(evt) {

    var root = this

            //In this case on evt.currentTarget.name i get 'padre' 
    var loprimero = evt.currentTarget.name;
    var loquesigue = "hijo";
    var todito = loprimero + loquesigue;


         //This console fine the name of instance i want gotoAndPlay
    console.log(todito);

        //This is i want to make work
        //This give me cannot read property 'gotoAndPlay' of undefined 
    root.todito.gotoAndPlay(1);

        //And this work fine
    this.padrehijo.gotoAndPlay(1);
}

When I define var loquesigue = this.padrehijo; without quotation marks it works fine. But remember I need to use this on some other parents symbols.


Solution

  • This is simply logging a string:

    console.log(todito);
    

    If you have an instance with that name, you can access it using brackets:

    var inst = root[todito];
    console.log(inst); // Does this work?
    

    One other note, you don't need to use bind in EaselJS, you can just use on() and its a little cleaner (docs) :)

    this.padre.on("click", nose, this);
    

    Cheers,