Jump to content

Tilemap forEach possible issue question


toms
 Share

Recommended Posts

I've been playing around with making a level editor using the tilemap. I wanted to iterate over each tile so I could generate some Tiled like JSON that is compatible with Phaser so I could load the level back in. I used the tilemap.forEach function to create an array of tile indexes to use for the layer data.

 

My code (typescript):

var indexes = [];myTilemap.forEach((tile: Phaser.Tile) => {   indexes.push(tile.index);}, this, 0, 0, myTilemap.width, myTilemap.height, theLayer);

This appeared to mostly work except the first tile wasn't actually a tile and its index was undefined. So I dug into the code and noticed the following in the tilemap.copy function used by the forEach function (master branch):

 

Tilemap.js line 1420:

this._results.push({ x: x, y: y, width: width, height: height, layer: layer });

The copy function adds this object to the _results before copying the tiles to _results, which is then the first none tile object I get in my forEach callback function (I say none tile because its index is undefined).

 

I also noticed that other functions that make use of the _results generated by the tilemap.copy function iterate through the results starting at index 1, which means they essentially miss out this none tile object at index 0. For example the tilemap.replace function (master branch):

 

Tilemap.js line 1575:

for (var i = 1; i < this._results.length; i++){    if (this._results[i].index === source)    {        this._results[i].index = dest;    }}

As far as I can see, this will skip that first none tile object.

 

So I have removed Tilemap.js line 1420 (where it adds the none tile object) and altered all the usages of _results when using copy and paste to start at 0 instead of 1, and it seams to fix my problem.

 

So basically, is this a valid fix? Or have I missed something? I feel I have missed something as line 1420 can't have been for no reason! Any way if this does make sense then I can submit a pull request, else can someone explain what I have missed? Thanks!

Link to comment
Share on other sites

  • 11 months later...
 Share

  • Recently Browsing   0 members

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