While using createStaticLayer I get an error regarding an undefined tileWidth

I am using phaser version 3.11.0.

When I am calling the line map.createStaticLayer('stone') I am getting the following error.

However right before I am calling console.log(map) the tileWidth appears to be defined as 32. Without the createStaticLayer() call the code runs with no errors and displays the background color defined in the config variable in index.html. Also if I purposely misspell the word stone I only get a warning that Cannot create tilemap layer, invalid layer ID given: stonze.

I have been trying to figure out how to fix it but I’m really stuck on this issue.

scene-one.js

class SceneOne extends Phaser.Scene {

    constructor () {
        super('SceneOne');
    }

    init() {
     
    }

    preload() {
        this.load.tilemapTiledJSON('map', 'assets/map/example_map.json');
        this.load.image('tileset' , 'assets/map/tilesheet.png')
    }

    create(data) {
        let map = this.add.tilemap('map');
        map.addTilesetImage('tilesheet', 'tileset'); // tilesheet is the key of the tileset in map's JSON file

        console.log("hi")
        console.log(map)
        map.createStaticLayer('stone')
        console.log(map)

    }

    update(time, delta) {
  
    }
}

var sceneOne = new SceneOne

index.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <script src="//cdn.jsdelivr.net/npm/phaser@3.11.0/dist/phaser.js"></script>
    <script src="js/scene-one.js" type="text/javascript"></script>
    <title>game</title>
</head>
<body>
    <div id='game'></div>    
    <script>
        const phaserConfig = {
            type: Phaser.AUTO,
            parent: "game",
            width: 1280,
            height: 720,
            backgroundColor: "#5DACD8",
            scene: [SceneOne]
        };

        const game = new Phaser.Game(phaserConfig);
    </script>     
</body>
</html>

:wave:

map.createStaticLayer('stone', map.tilesets[0]);

or you can pass the result of addTilesetImage().

1 Like

@samme thanks your suggestion of including map.tileset[0] as a parameter worked. Can you explain what I was missing, because I have seen plenty of examples that didn’t include this.

I also tried googling the function and am looking at the following URLs but I am unable to quickly find the description for this function.

https://photonstorm.github.io/phaser3-docs/Phaser.Tilemaps.StaticTilemapLayer.html
https://photonstorm.github.io/phaser3-docs/Phaser.Tilemaps.Tilemap.html

The tileset argument is required. Since Phaser v3.50 there are no static vs. dynamic tilemap layers, so the method is createLayer().

2 Likes

Thank you so much for the help!