如何仅删除CreateJS中的形状?在我的示例中,我创建了一些名为的函数的正方形createSquare
。我的目标是拥有一个函数或click事件,该函数或函数仅删除被单击的正方形。
我尝试过事件监听器和单击,但没有运气。我已经注释掉了我尝试使用的代码。
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();
CreateJS中的事件处理程序将传递一个事件对象。鼠标事件会收到MouseEvent。
事件的目标将是单击的对象。
square.on("click", function(evt) {
stage.removeChild(evt.target);
stage.update();
});
创建方形后,您需要将侦听器添加到每个方形。
另外,您也可以听一次舞台。
stage.on("click", function(evt) {
stage.removeChild(evt.target);
stage.update();
});
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句