Unable to load tilesets and maps into my game

Hello, I am new to phaser and game development.

I followed the below tutorial.

https://medium.com/@michaelwesthadley/modular-game-worlds-in-phaser-3-tilemaps-1-958fc7e6bbd6

I downloaded and Tiled software and made a simple map with a tileset I got from OpenGameArt.org. Unfortunately, nothing gets loaded on the browser screen, I just see a black square instead of the map. I find no errors in the console. I am running this using XAMPP in Windows 10.

I will paste all my code here, let me know if you find anything wrong.

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <script src="https://cdn.jsdelivr.net/npm/phaser@3.15.1/dist/phaser-arcade-physics.min.js"></script>
</head>
<body>
    <script src="index.js" type="text/javascript"></script>
</body>
</html>

The is the index.js file

const config = {
        type: Phaser.AUTO, // Which renderer to use
        width: 800, // Canvas width in pixels
        height: 600, // Canvas height in pixels
        parent: "game-container", // ID of the DOM element to add the canvas to
        scene: {
          preload: preload,
          create: create,
          update: update
        }
      };
      const game = new Phaser.Game(config);
      function preload() {
        // Runs once, loads up assets like images and audio
        this.load.image("tiles", "assets/tilesets/GoldBricks.png");
        this.load.tilemapTiledJSON("map", "assets/tilemaps/mario.json");
      }
      function create() {
        // Runs once, after all assets in preload are loaded
        const map = this.make.tilemap({ key: "map" });
        const tileset = map.addTilesetImage("GoldBricks", "tiles");
        // Parameters: layer name (or index) from Tiled, tileset, x, y
        const belowLayer = map.createStaticLayer("Tile Layer 1", tileset, 0, 0);
      }

      function update(time, delta) {
        // Runs once per frame for the duration of the scene
      }

Iā€™m not sure but you can try adding a div tag with ID ā€œgame-containerā€. Hope that helps :slight_smile:

Thanks for the response, but it didnā€™t work. I was able to see the canvas being rendered even without the div with ID ā€œgame-containerā€ but the canvas is just a black rectangle 800 x 600 pixels.

I had forgotten to add doctype to the html tag, I added it and there still no sign of the map.

There is also a warning in the console which I didnā€™t mention before.

phaser.js:43859 The AudioContext was not allowed to start. It must be resumed (or created) after a user gesture on the page. Autoplay policy in Chrome - Chrome for Developers

Actually now I am getting an error in the console.

phaser.js:74806 Uncaught TypeError: Cannot read property ā€˜0ā€™ of undefined
at StaticTilemapLayer.upload (phaser.js:74806)
at StaticTilemapLayerWebGLRenderer [as renderWebGL] (phaser.js:122959)
at WebGLRenderer.render (phaser.js:65133)
at CameraManager.render (phaser.js:114533)
at Systems.render (phaser.js:27184)
at SceneManager.render (phaser.js:46818)
at Game.step (phaser.js:109346)
at TimeStep.step (phaser.js:106091)
at step (phaser.js:66488)

Please post the assets (map and tileset image).

Whatā€™s on this line?

Also you may want to switch to the unminified script, https://cdn.jsdelivr.net/npm/phaser@3.15.1/dist/phaser-arcade-physics.js.

thisā€¦

If you click on phaser.js:74806 in the console, what does it show you?

Could you upload GoldBricks.png and mario.json?

mario.json (13.9 KB)

The error is not showing now, anyway, have a look at the uploaded the files.

So this is a common problem :wink:

Your tilemap layer is taller than the game canvas so the visible tiles are out of sight.

this.cameras.main.centerOn(800, 1200); 

would get you closer. You can add camera controls (cursor keys), see the examples.

1 Like