easeljs将位图添加到不同位置的多个时间段

时间:2013-12-04 17:21:48

标签: javascript easeljs

我刚开始使用easeljs库。我创建了一个带舞台的画布,它可以工作。

我想将位图添加为按钮,并将它们并排添加到舞台的底部。

我尝试了以下内容,但它将位图放在彼此的顶部。

var button = new createjs.Bitmap(queue.getResult("largebrick"));
        var contentWidth = document.getElementById("content").offsetWidth;
        var contentHeight = document.getElementById("content").offsetHeight;
        var container = new createjs.Container();
        stage.addChild(container);

        stage.enableMouseOver(10);
        for(var i = 0; i <10; i++){
            button.x = 0 + button.getBounds().width * i;
            button.y = contentHeight - button.getBounds().height;

            button.addEventListener("mouseover", function() { document.body.style.cursor = 'pointer'; });
            button.addEventListener("mouseout", function() { document.body.style.cursor = 'default'; });
            button.addEventListener("click", function(event) {addBricks(button); });
            container.addChild(button);

        }

我希望你们中的一些人可以提供帮助。

1 个答案:

答案 0 :(得分:2)

这里有几件事。您需要在循环内创建按钮的新实例,否则您只是在每次迭代时移动按钮的原始实例。在this example中,我通过使用形状简化了一切,但您可以轻松地对Bitmap执行相同的操作。

var xPos = 0;
for(var i = 0; i < 10; i++){
    var button = new createjs.Bitmap(queue.getResult("largebrick"));
    button.cursor = "pointer";

    button.x = xPos;
    button.y = contentHeight - 50;
    xPos += 60;
    container.addChild(button);

}

此外,createjs支持将游标定义为显示对象的属性,如此

button.cursor = "pointer";

这应该会简化你的代码。