I've been working on constructing a random map generator by utilizing a grid composed of tiles within a canvas. In my process, I'm investigating the properties of each tile tiles[i]
before handling tiles[i-1]
. While this procedure seems to function correctly with "ordinary" objects (method 1), it encounters an issue with "2d" objects (method 2) where the value of tiles[i-1].tileID
displays as tiles[i].tileID
.
for (i=0; i<40; i++) {
tiles[i] = { tileID: i}; // METHOD 1
tiles[i]=c.getContext("2d") // METHOD 2
tiles[i].tileID = i // METHOD 2
if(i>0) {
console.log("my tile ID is", tiles[i].tileID,
". The tile ID of the tile before me is", tiles[i-1].tileID)
}
}
When employing method 1 (and eliminating the two lines from method 2), the console outputs the anticipated outcome:
my tile ID is 12 . The tile ID of the tile before me is 11
However, for method 2, the result differs:
my tile ID is 12 . The tile ID of the tile before me is 12
Is there a rationale behind this behavior (or perhaps another approach I should consider) prior to exploring alternative solutions? Thanks!