Animate CC - 使用 var 字符串调用实例

Animate CC - Call instance with var Strings

我正在尝试创建一个功能,当我单击另一个符号动画 (padre) 时播放符号动画 (hijo)。我想要一个函数多次使用它们,所以我正在尝试自动化。

我正在获取单击按钮的实例名称 "padre" 并尝试在本例 "hijo" 之后添加一些字符串。我创建了一些变量,但是当我使用字符串值时它不起作用。

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);
}

当我定义 var loquesigue = this.padrehijo; 时不带引号,它工作正常。但请记住,我需要在其他一些 parents 符号上使用它。

这只是记录一个字符串:

console.log(todito);

如果您有一个具有该名称的实例,您可以使用方括号访问它:

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

另一个注意事项,你不需要在 EaselJS 中使用 bind,你可以只使用 on() 并且它更干净一点(docs):)

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

干杯,