phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
37.16k stars 7.1k forks source link

tilemap object position doesn't move if parent container moves position #6864

Closed HawkenKing closed 4 months ago

HawkenKing commented 4 months ago

Version

Description

Expectation of tilemap and other objects to move when added to a container and moving the container. Result: other objects move with container, tilemap is stays where it was generated.

Example Test Code

` this.gameContainer = this.add.container(0, 0);

    const map = this.make.tilemap({ key: 'map' });
    const tileset = map.addTilesetImage('tiles', 'tiles');
    const groundLayer = map.createLayer('Tile Layer 1', tileset);

    this.gameContainer.add(groundLayer);

    this.gameContainer.setPosition(20,20);`

Additional Information

In this screen shot, a sprite is also added at 0,0 then the container is moved to 20,20. The sprite moves with the container but the tilemap stays at its initial added position of 0,0.

Screenshot 2024-07-15 at 10 59 10

greptile-apps[bot] commented 4 months ago

For internal use only

To resolve the issue where a Tilemap object doesn't move when its parent container moves, you need to ensure that the TilemapLayer correctly updates its position based on the container's position. This can be done by overriding the Container.addHandler method to account for TilemapLayers. Here are the steps:

  1. Open the Container.js file.
  2. Modify the addHandler method to include a check for TilemapLayers and update their positions accordingly.
Container.addHandler = function (child) {
    if (child.type === 'TilemapLayer') {
        child.setPosition(this.x + child.x, this.y + child.y);
    }
    child.addedToScene();
};

This ensures that when a TilemapLayer is added to a container, its position is updated relative to the container's position.

References

/changelog/3.80/CHANGELOG-v3.80.md /changelog/3.60/Tilemap.md /changelog/3.14/CHANGELOG-v3.14.md /changelog/3.60/Container.md

#### About Greptile This response provides a starting point for your research, not a precise solution. Help us improve! Please leave a ๐Ÿ‘ if this is helpful and ๐Ÿ‘Ž if it is irrelevant. [Ask Greptile](https://app.greptile.com/chat/github/phaserjs/phaser/master) ยท [Edit Issue Bot Settings](https://app.greptile.com/apps/github)
photonstorm commented 4 months ago

Tilemaps should not be added to Containers. They are stand-alone rendering objects. We will add this to the documentation, but you shouldn't use them in this way.