Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tilemap.copy() Bug #6188

Closed
Arkyris opened this issue Aug 3, 2022 · 3 comments
Closed

tilemap.copy() Bug #6188

Arkyris opened this issue Aug 3, 2022 · 3 comments

Comments

@Arkyris
Copy link

Arkyris commented Aug 3, 2022

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
Copy link
Author

Arkyris commented Aug 3, 2022

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
Copy link
Author

Arkyris commented Aug 15, 2022

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

@photonstorm
Copy link
Collaborator

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants