I’d need a https://codepen.io/ with your actual game, otherwise I’m just guessing. But maybe
create(){
const rows = [1,0,1,0,0,0];
for(let i = 0; i < rows.length; i+=1){
// ... more code
let idx = i; // i is the index that you want, right?
cell.on('pointerdown', () => {
// ... more code
}
// ... more code
}
Again, I am just guessing here so can you please make a codepen?
Another way to put this. The problem is the individual cell.on does not treat the selected cell as an individual object with an index. Is there a way to get the index of the clicked on cell? (one way would be to get the x/y co-ordinates of the clicked cell and convert that back into the index number, which seems to be the way used by Emanuele Feronato in his tutorials… is there another way? )
var row = [1, 0, 1, 0, 0, 0];
for(var i = 0; i < row.length; i++){
var x = 150 + (i * 40);
var cell = this.add.circle(x, 100, 10, 0xffffff).setInteractive();
console.log(i); //this is correct: logs 0, 1, 2, 3, 4, 5
cell.i = i;
console.log(cell.i); //this is correct: logs 0, 1, 2, 3, 4, 5
cell.on('pointerdown', function() {
console.log(cell.i); //always logs "5"
});