phaser icon indicating copy to clipboard operation
phaser copied to clipboard

tilemap.copy() Bug

Open Arkyris opened this issue 1 year ago β€’ 2 comments

Version

  • Phaser Version: 3.55.2
  • Operating system: Windows 10
  • Browser: N/A

Description

When trying to copy more then half the map tilemap.copy() does not work as expected. It works in one direction but not the other. If the map is 5x5 and you want to move columns 2-5 to the left one it works fine. If you want to move columns 1-4 to the right one it just replicates column 1 over and over. This happens when you pass the half way point on a map. So on an 11x11 moving tiles 1-5 to over to column 6 works perfectly fine. Moving columns 1-6 over to column 5 is when it starts to mess up.

Example Test Code

on an 11x11 map this.tmap.copy(0, 0, 5, 25, 6, 0); works.

this.tmap.copy(0, 0, 6, 25, 5, 0); where issues begin

Additional Information

Arkyris avatar Aug 03 '22 00:08 Arkyris

I fixed? it by having getTilesWithin() construct an array of new Phaser.Tilemaps.Tiles that copy the tiles instead of referencing existing ones. Not sure if there's a better way to do it but it works.

Arkyris avatar Aug 03 '22 16:08 Arkyris

Sorry kinda new to github etiquette. This was my fix

`function myGetTilesWithin(tileX, tileY, width, height, layer) { if (tileX === undefined) { tileX = 0; } if (tileY === undefined) { tileY = 0; } if (width === undefined) { width = layer.width; } if (height === undefined) { height = layer.height; }

// Clip x, y to top left of map, while shrinking width/height to match.
if (tileX < 0) {
    width += tileX;
    tileX = 0;
}

if (tileY < 0) {
    height += tileY;
    tileY = 0;
}

// Clip width and height to bottom right of map.
if (tileX + width > layer.width) {
    width = Math.max(layer.width - tileX, 0);
}

if (tileY + height > layer.height) {
    height = Math.max(layer.height - tileY, 0);
}

var results = [];

for (var ty = tileY; ty < tileY + height; ty++) {
    for (var tx = tileX; tx < tileX + width; tx++) {
        var tile = new Phaser.Tilemaps.Tile(layer.data[ty][tx].layer, layer.data[ty][tx].index, layer.data[ty][tx].x, layer.data[ty][tx].y, layer.data[ty][tx].width, layer.data[ty][tx].height, layer.data[ty][tx].baseWidth, layer.data[ty][tx].baseHeight);

        if (tile !== null) {
            results.push(tile);
        }
    }
}

return results;

}`

I just copied the phaser getTilesWithin() But instead of var tile = layer.data[ty][tx]; I did var tile = new Phaser.Tilemaps.Tile(layer.data[ty][tx].layer, layer.data[ty][tx].index, layer.data[ty][tx].x, layer.data[ty][tx].y, layer.data[ty][tx].width, layer.data[ty][tx].height, layer.data[ty][tx].baseWidth, layer.data[ty][tx].baseHeight);

not sure why the first part of the large codeblock wont go into the code block

Arkyris avatar Aug 15 '22 16:08 Arkyris

Yeah it happens because the copy operation works on the same array as the 'rect', so it overwrites the content as it's being copied, leading to corruption. We have fixed this and the fix has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.

photonstorm avatar Nov 28 '22 13:11 photonstorm