Jump to content

Cannot read property '2' of undefined


coelacanth
 Share

Recommended Posts

Here is the code from an example. I have separte tiles images. 

<!doctype html>
<html>
<head>
    <script src="phaser.min.js"></script>
</head>
<body>

<script>
   
var game = new Phaser.Game(1024, 1024, Phaser.CANVAS, 'phaser-example', { preload: preload, create: create });

function preload() {

    game.load.tilemap('map', 'bsdmap.json', null, Phaser.Tilemap.TILED_JSON);

    game.load.image('terrain', 'terrain.png');
    game.load.image('p1', 'p1.png');
    game.load.image('p2', 'p2.png');
    game.load.image('bg_tile', 'bg_tile.png')

    //game.load.image('phaser', 'assets/sprites/arrow.png');
    //game.load.spritesheet('coin', 'assets/sprites/coin.png', 32, 32);

}


var map;
var layer;


function create() {

    map = game.add.tilemap('map');

    map.addTilesetImage('terrain');
    map.addTilesetImage('p1');
    map.addTilesetImage('p2');
    map.addTilesetImage('bg_tile');
    
   

    layer = map.createLayer('Tile Layer 1');

    layer.resizeWorld();

}

</script>
</body>
</html>

Here is a tiled map in json format

{ "height":32,
 "infinite":false,
 "layers":[
        {
         "data":[2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,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, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
         "height":32,
         "name":"Tile Layer 1",
         "opacity":1,
         "type":"tilelayer",
         "visible":true,
         "width":32,
         "x":0,
         "y":0
        }],
 "nextobjectid":1,
 "orientation":"orthogonal",
 "renderorder":"right-down",
 "tiledversion":"1.1.2",
 "tileheight":32,
 "tilesets":[
        {
         "columns":0,
         "firstgid":1,
         "grid":
            {
             "height":1,
             "orientation":"orthogonal",
             "width":1
            },
         "margin":0,
         "name":"tileset",
         "spacing":0,
         "tilecount":4,
         "tileheight":32,
         "tileproperties":
            {
             "0":
                {
                 "blocked":""
                },
             "1":
                {
                 "blocked":""
                },
             "2":
                {
                 "platform":""
                },
             "3":
                {
                 "platform":""
                }
            },
         "tilepropertytypes":
            {
             "0":
                {
                 "blocked":"string"
                },
             "1":
                {
                 "blocked":"string"
                },
             "2":
                {
                 "platform":"string"
                },
             "3":
                {
                 "platform":"string"
                }
            },
         "tiles":
            {
             "0":
                {
                 "image":"..\/..\/..\/..\/Users\/Andrew\/Desktop\/totem burners assets\/tiles2\/terrain.png",
                 "imageheight":32,
                 "imagewidth":32
                },
             "1":
                {
                 "image":"..\/..\/..\/..\/Users\/Andrew\/Desktop\/totem burners assets\/tiles2\/bg_tile.png",
                 "imageheight":32,
                 "imagewidth":32
                },
             "2":
                {
                 "image":"..\/..\/..\/..\/Users\/Andrew\/Desktop\/totem burners assets\/tiles2\/p1.png",
                 "imageheight":32,
                 "imagewidth":32
                },
             "3":
                {
                 "image":"..\/..\/..\/..\/Users\/Andrew\/Desktop\/totem burners assets\/tiles2\/p2.png",
                 "imageheight":32,
                 "imagewidth":32
                }
            },
         "tilewidth":32
        }],
 "tilewidth":32,
 "type":"map",
 "version":1,
 "width":32
}

All the tiles are embedded in Tiled.

But I got an error: 

Uncaught TypeError: Cannot read property '2' of undefined
    at Object.parseTiledJSON (phaser.min.js:3)
    at Object.parse (phaser.min.js:3)
    at new i.Tilemap (phaser.min.js:3)
    at i.GameObjectFactory.tilemap (phaser.min.js:3)
    at Object.create (index.html:33)
    at i.StateManager.loadComplete (phaser.min.js:3)
    at i.Loader.finishedLoading (phaser.min.js:3)
    at i.Loader.processLoadQueue (phaser.min.js:3)
    at i.Loader.asyncComplete (phaser.min.js:3)
    at i.Loader.fileComplete (phaser.min.js:3)

 

Link to comment
Share on other sites

But in what you pasted above, you're missing the end syntax on your game variable. I'm just checking if that is present in what you're trying to run. 

this line: var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });

Link to comment
Share on other sites

Quote

But in what you pasted above, you're missing the end syntax on your game variable. I'm just checking if that is present in what you're trying to run. 

this line: var game = new Phaser.Game(800, 600, Phaser.AUTO, 'phaser-example', { preload: preload, create: create });

Everything is ok there. I still get this error.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...