我只是想为每个形状设置一个点击事件,我希望能够根据他们的 id 向我显示它是基于哪个正方形。
var stage = new Kinetic.Stage({
container: 'container',
width: 700,
height: 700,
});
var squareLayer;
var boardBlankArray;
function drawBoard() {
var squareLayer = new Kinetic.Layer();
var initialSquares = 64;
var squareX = 20;
var squareY = 20;
boardBlankArray = [];
for (i = 0; i < initialSquares; i++) {
var square = new Kinetic.Rect({
x: squareX,
y: squareY,
width: 50,
height: 50,
fill: "blue",
stroke: "black",
strokeWidth: 2,
id: "squares" + i
});
squareX += 60; //update squares X (horizontal) position
if (squareX > 450) {
squareX = 20;
squareY += 60;
}
boardBlankArray[i] = square;
squareLayer.add(boardBlankArray[i]);
boardBlankArray[i].on("click", function() {
alert(boardBlankArray[i].getId());
});
}
stage.add(squareLayer);
}
drawBoard();
我做了小提琴,由于某种原因,它说 boardBlankArray 是未定义的,但显然是。此外,当我在计算机上本地执行此操作时,我没有收到该错误。相反,它只是提醒所有方块的相同 id。
任何帮助将不胜感激。