phaser icon indicating copy to clipboard operation
phaser copied to clipboard

GetLayer can have undefined behaviour after removing a layer from a tilemap

Open JaroVDH opened this issue 1 year ago • 0 comments

Version

  • Phaser Version: 3.55.2
  • Operating system: Any
  • Browser: Any

Description

Removing a Tilemap Layer from a Tilemap through the Layer itself causes some Tilemap functionality to break. One example is map.getLayer/map.getLayerIndex, which indirectly breaks other stuff like worldToTileX

I believe it's happening because the Tilemap's currentLayerIndex is getting out of sync with the actual layer count.

Example Test Code

const map = this.make.tilemap({ data: [[0]]}),
        tileset = map.addTilesetImage('tiles'),
        layer = map.createLayer(0, tileset, 0, 0);

    layer.destroy(true);

    // Exception
    map.worldToTileX(1);

Inside map.worldToTileX(), the getLayer call returns undefined, and worldToTileX only checks for null. It returns undefined because it's trying to read a currentLayerIndex higher than the current layer count.

Another easy way to reproduce this is by slightly changing this example and adding this somewhere near the end in the create function:

waterLayer.destroy(true);
map.worldToTileX(1); // crash

That also shows it's not just because of removing the last layer as the example has 4.

JaroVDH avatar Apr 09 '23 17:04 JaroVDH