CreateJS(Animate CC)点击获取电影实例名称

时间:2016-10-27 18:26:12

标签: javascript jquery-animate target createjs movieclip

我已经搜索了这个问题的解决方案,虽然我找到了几个旧帖子,但它们似乎都没有解决问题,而是找到了一些适合该特定任务的独特解决方法。所以我希望再次提起这个问题,看看是否有人可以提出更普遍的建议。

我有一个单独的功能,内部有一个开关,可以听到按下的影片剪辑,并且根据影片剪辑的名称,需要采取某些措施。由于CreateJS似乎没有拿起动画片段名称,我已将自己的动画片段分配给各个动画片段,但是,如何轻松查看哪个片段被点击?在AS3中,使用 e.target.name

可以轻松完成

这是我的代码片段:

this.parentMovie.button1.name = "button1";
this.parentMovie.button2.name = "button2";
this.parentMovie.button3.name = "button3";

this.parentMovie.button1.addEventListener("click", onClick.bind(this));
this.parentMovie.button2.addEventListener("click", onClick.bind(this));
this.parentMovie.button3.addEventListener("click", onClick.bind(this));

function onClick(e)
{
    console.log(this.parentMovie.button1.name);    // returns the name of the movieclip just fine, so I know at least that is carried over.

    switch(/* what do I need to put here?? */) // in AS3 it was e.target.name and it worked great.  In JS it returns as null
    {
        case "button1":
            // do something
            break;

        case "button2":
            // do something
            break;

        case "button3":
            // do something
            break;
    }
}

非常感谢!

1 个答案:

答案 0 :(得分:3)

name属性不会自动创建(尽管它应该是)。你的switch语句应该很简单,只需使用变量引用:

function onClick(e)
{
    console.log(this.parentMovie.button1.name);    // returns the name of the movieclip just fine, so I know at least that is carried over.

    switch(e.currentTarget) // Use currentTarget since the button might have children
    {
        case this.parentMovie.button1:
            // do something
            break;

        case this.parentMovie.button2:
            // do something
            break;

        case this.parentMovie.button3:
            // do something
            break;
    }
}

它应该完成你想要做的事情。