我为每个对象分配了一个唯一的 ID,并在单击它时使用以下内容将其删除:
r = r + 1;
var mousePos = stage1.getMousePosition();
rectA1 = new Kinetic.Rect({
x: mousePos.x - rOffset,
y: mousePos.y - rOffset,
width: 0,
height: 0,
stroke: 'red',
strokeWidth: 4,
id:"rectA" + r
});
rectA1.setListening(true);
myRect1[r] = rectA1;
background1.add(myRect1[r]);
//start point and end point are the same
rectA1.setX(mousePos.x - rOffset);
rectA1.setY(mousePos.y - rOffset);
rectA1.setWidth(0);
rectA1.setHeight(0);
moving = true;
background1.drawScene();
myRect1[r].on("click", function () {
this.remove();
});
我的解决方案基于这个很好的答案:How to select an object in kinetic.js?