I am simply trying to set up a click event for each shape and I want to be able to display to me which square it is based off of their 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();
I made the fiddle and for some reason it is saying the boardBlankArray is undefined but it clearly is. Also when I do this locally on my computer I do not get that error. Instead it just alerts the same id for all of the squares.
Any help would be greatly appreciated.
Try this.attrs.id instead of boardBlankArray[i].getId() for shape Id
Fixed Output:
http://jsfiddle.net/tSvmd/1/