When creating multiple new TileMap
objects, there is a problem with re-using the same array as an input argument:
var gridAsString = 'B\nP';
var gridAsArray = [['B'], ['P']];
// Using a string works correctly:
var blocks = new TileMap(gridAsString, { B: Block });
var pots = new TileMap(gridAsString, { P: Pot });
// But using an array is broken:
var blocks = new TileMap(gridAsArray, { B: Block });
var pots = new TileMap(gridAsArray, { P: Pot });
I believe this is because of the code overwriting the grid
argument by reference. By the time the pots
collection is being created, gridAsArray
is already partly filled with Block
objects.
To fix this, I just added one line immediately below the function start:
...
function TileMap(grid, map, options) {
var grid = JSON.parse(JSON.stringify(grid));
// Setup and options
...
I think this is all the cloning that is necessary. I'm not a Javascript native, so there may be something I've overlooked.
Thanks.