admin管理员组

文章数量:1323323

I'm working with Paser.js on a Meteor.js server.

It worked wperfectly until I try to use tiled maps as described here.

Here is my code :

JS :

    if (Meteor.isClient) {
  Template.Game.onCreated(function()
  {
    var game = new Phaser.Game(800, 600, Phaser.AUTO, '', {
       preload: preload,
       create: create,
       update: update
    });
    var map;
    var backgroundLayer;
    var blockLayer;
    var bg;
function preload()
{
  // load all game assets
  // images, spritesheets, atlases, audio etc..
  game.load.tilemap('myTilemap', 'assets/tilemaps/scifi.json', null, Phaser.Tilemap.TILED_JSON);
  game.load.image('myTileset', "assets/tilemaps/scifi_platformTiles_32x32.png");
}

function create()
{
  map = game.add.tilemap('myTilemap');
  map.addTilesetImage('scifi_platformTiles_32x32', 'myTileset');

  backgroundLayer = map.createLayer('background');
  blockLayer = map.createLayer('blocklayer');
}

function update()
{

}
  });
}

HTML :

<head>
    <meta charset="UTF-8" />
    <title>Phaser - Making your first game, part 1</title>
    <script type="text/javascript" src="phaser.min.js"></script>
    <style type="text/css">
        body {
            margin: 0;
        }
    </style>
</head>

<body>
  <h1>Wele to my first Phaser game!</h1>

  {{> Game}}
</body>

<template name="Game">
  <div id="phaserCanvas"></div>
</template>

And, when I try it on localhost:3000, I get :

Uncaught TypeError: Cannot read property '0' of undefined

From phaser.min.js:15. The line which generate that warning is

blockLayer = map.createLayer('blocklayer');

It seems that phaser can correctly read the 'background' layer information from the scifi.json, but not the 'blocklayer' one.

Here is an extract from the scifi.json :

{ "height":20,
 "layers":[
        {
         "pression":"zlib",
         "data": "[Some very long hashed key...]",
         "encoding":"base64",
         "height":20,
         "name":"background",
         "opacity":1,
         "type":"tilelayer",
         "visible":true,
         "width":20,
         "x":0,
         "y":0
        }, 
        {
         "pression":"zlib",
         "data":"[Some very long hashed key...]",
         "encoding":"base64",
         "height":20,
         "name":"blocklayer",
         "opacity":1,
         "type":"tilelayer",
         "visible":true,
         "width":20,
         "x":0,
         "y":0
        }],
 "nextobjectid":1,
[...]

And I'm stil unable to find out what's the problem... Has anyone faced that before ?

More informations :

  • I use Atom as IDE

  • I tried with Phaser v2.0.1 and Phaser v2.4.2

Thanks you.

I'm working with Paser.js on a Meteor.js server.

It worked wperfectly until I try to use tiled maps as described here.

Here is my code :

JS :

    if (Meteor.isClient) {
  Template.Game.onCreated(function()
  {
    var game = new Phaser.Game(800, 600, Phaser.AUTO, '', {
       preload: preload,
       create: create,
       update: update
    });
    var map;
    var backgroundLayer;
    var blockLayer;
    var bg;
function preload()
{
  // load all game assets
  // images, spritesheets, atlases, audio etc..
  game.load.tilemap('myTilemap', 'assets/tilemaps/scifi.json', null, Phaser.Tilemap.TILED_JSON);
  game.load.image('myTileset', "assets/tilemaps/scifi_platformTiles_32x32.png");
}

function create()
{
  map = game.add.tilemap('myTilemap');
  map.addTilesetImage('scifi_platformTiles_32x32', 'myTileset');

  backgroundLayer = map.createLayer('background');
  blockLayer = map.createLayer('blocklayer');
}

function update()
{

}
  });
}

HTML :

<head>
    <meta charset="UTF-8" />
    <title>Phaser - Making your first game, part 1</title>
    <script type="text/javascript" src="phaser.min.js"></script>
    <style type="text/css">
        body {
            margin: 0;
        }
    </style>
</head>

<body>
  <h1>Wele to my first Phaser game!</h1>

  {{> Game}}
</body>

<template name="Game">
  <div id="phaserCanvas"></div>
</template>

And, when I try it on localhost:3000, I get :

Uncaught TypeError: Cannot read property '0' of undefined

From phaser.min.js:15. The line which generate that warning is

blockLayer = map.createLayer('blocklayer');

It seems that phaser can correctly read the 'background' layer information from the scifi.json, but not the 'blocklayer' one.

Here is an extract from the scifi.json :

{ "height":20,
 "layers":[
        {
         "pression":"zlib",
         "data": "[Some very long hashed key...]",
         "encoding":"base64",
         "height":20,
         "name":"background",
         "opacity":1,
         "type":"tilelayer",
         "visible":true,
         "width":20,
         "x":0,
         "y":0
        }, 
        {
         "pression":"zlib",
         "data":"[Some very long hashed key...]",
         "encoding":"base64",
         "height":20,
         "name":"blocklayer",
         "opacity":1,
         "type":"tilelayer",
         "visible":true,
         "width":20,
         "x":0,
         "y":0
        }],
 "nextobjectid":1,
[...]

And I'm stil unable to find out what's the problem... Has anyone faced that before ?

More informations :

  • I use Atom as IDE

  • I tried with Phaser v2.0.1 and Phaser v2.4.2

Thanks you.

Share edited Aug 16, 2015 at 16:02 David Panart asked Aug 16, 2015 at 15:45 David PanartDavid Panart 6566 silver badges21 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

Seems the problem came from Tiled : the hashed key was pressed with Zlib, though it should not be pressed at all as phaser do not support it yet.

In tiled, go to Map -> Map Properties There you will find Tile Layer Format. Change this to Base64 (unpressed) and it should work :)

本文标签: javascriptPhaserjscannot read property 39039 on tiled map layerStack Overflow