Jump to content

Loop through all tiles in a map


owen
 Share

Recommended Posts

I seem to be unable to loop through the tiles in a map using forEach.  It gets some of the way through and then falls over with an error.

 

Here is a simple example of what I'm attempting.  I think it should be simple but I'm a newbie so please forgive me but tell me what dumb mistake I am making.

 

    console.log("STARTED test loop through tiles");    map.forEach(function (tile) {        console.log("Found a tile");    }, this, 0, 0, map.width, map.height, layer);    console.log("FINISHED test loop through tiles"); // does not get reached!!!
This outputs a lot of "Found a tile" lines (as expected) but then it fails to reach the end of the loop.  It dies with:
 
Uncaught TypeError: cannot read property 'y' of null
 
What am I doing wrong please?
 
EDIT: map.tiles.forEach seems to work a little better than map.forEach in that it completes the loop... however both methods don't give a proper reference to the tile in question.  In both cases the tile.index comes back as undefined
console.log("STARTED another test loop through tiles");map.tiles.forEach(function (tile) {       console.log("Found a tile with index="+tile.index); // shows "Found a tile with index=undefined"}, this, 0, 0, map.width, map.height, layer);console.log("FINISHED another test loop through tiles"); // DOES get reached!!!
 
EDIT: another interesting test reveals that the objects coming back in the loop are sets of 3 numbers comma delimited. eg "32,0,0" - what is this and how can I use it to get the tile object I want?    
console.log("STARTED yet another test loop through tiles");    map.tiles.forEach(function (tile) {        var o = tile;        console.log("Found a tile "+o); // gives something like "Found a tile 32,0,0" - what is this???    }, this, 0, 0, map.width, map.height, layer);    console.log("FINISHED yet another test loop through tiles"); // DOES get reached!!!
 
ANOTHER EDIT: The 3 numbers seem to be the X coordinate, Y coordinate and Tilesheet number.  So maybe I can use map.getTileWorldXY to determine which tile is at those coordinates, and thus get the tile object from that.  In the test below I get a line output in the console per tile on the map - so that seems to be correct.  HOWEVER, the index = 101 for every tile.  Which cannot be right.  (Just so happens tile 101 is the top-left of my map)
console.log("**** STARTED loop through map.tiles using getTileWorldXY on results ****");    //map.tiles.forEach(function (tile) {            map.tiles.forEach(function (tile) {       var gotTile = map.getTileWorldXY(parseInt(tile[0] / TILE_SIZE), parseInt(tile[1] / TILE_SIZE), TILE_SIZE, TILE_SIZE, layer);       if (gotTile) { console.log("found tile with index " + gotTile.index); }    }, this, 0, 0, map.width, map.height, layer);    console.log("**** FINISHED yet another test loop through tiles ****"); // DOES get reached!!!
 
YET ANOTHER EDIT:  It looks like it has to be map.forEach  instad of map.tiles.forEach in order to get a reference to the tiles within the loop.  However, map.forEach crashes out with the above mentioned error.  So I am back to square one.  I will stop posting all these edits now until an answer is found either by myself or somebody else :-)
 
For now I've resorted to a "try...catch" around the error.
 
Cheers
Owen.
Link to comment
Share on other sites

  • 3 weeks later...

I don't know if its too late but i had also problems to iterate through the map.

this error you have on

 map.forEach(function (tile) {        console.log("Found a tile"); }

only happens when you have null objects in your map(tiles where you don't add any object from your tileset)

it looks like this foreach tries to acces them after the iteration over all tiles is done to get the porterties or something... i dont know it.

 

this works without any error for me even with null objects in the map.

for(var y = 0; y < map.height; ++y){   for(var x = 0; x < map.width; ++x){      var tile = map.getTile(x, y);      console.log(tile);   }}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...