0
如何删除CreateJS中的形状?就我的例子而言,我用一个叫做createSquare
的函数创建了几个正方形。我的目标是有一个功能或点击事件,只删除被点击的方块。使用CreateJS删除形状
我尝试过事件监听器,并点击,并没有运气。我已经注释了我尝试使用的代码。
Here is a link to a working fiddle.
JS如下:
var canvas;
var stage;
var square;
function init() {
canvas = document.getElementById("canvas");
stage = new createjs.Stage(canvas);
}
function createSquare(){
square = new createjs.Shape();
square.graphics.beginFill("red").drawRect(0, 0, 50, 50)
square.x = Math.random() * canvas.width;
square.y = Math.random() * canvas.height;
stage.addChild(square);
stage.update();
}
// This code should remove the squares
/*
square.on("click", function(evt) {
stage.removeChild(this);
});
*/
window.onload = init();
createSquare();
createSquare();
createSquare();
createSquare();
createSquare();
createSquare();
一种好奇的小提琴:https://jsfiddle.net/w1vssodr/3/ – vzwick
这是有道理的,并清除了我的困惑。如果我添加其他形状,将事件添加到每个方块是最好的解决方案吗?例如,如果我想为一个正方形和一个圆圈做些微不同的事件呢?你的其他解决方案的小提琴https://jsfiddle.net/w1vssodr/5/ – DRB
我会推荐一个单一的事件处理程序。你可以很容易地将一个属性放到指明它是什么的形状上,然后在点击处理程序中检查它。这里是一个更新的小提琴,展示了如何删除正方形,并减少了圆圈。 https://jsfiddle.net/lannymcnie/w1vssodr/6/ – Lanny