【问题标题】:Phaser.js : cannot read property '0' on tiled map layerPhaser.js:无法读取平铺地图图层上的属性“0”
【发布时间】:2015-11-09 07:11:17
【问题描述】:

我正在 Meteor.js 服务器上使用 Paser.js。

在我尝试使用 here 所述的平铺地图之前,它一直运行良好。

这是我的代码:

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>Welcome to my first Phaser game!</h1>

  {{> Game}}
</body>

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

而且,当我在 localhost:3000 上尝试时,我得到:

未捕获的类型错误:无法读取未定义的属性“0”

来自 phaser.min.js:15。产生该警告的行是

blockLayer = map.createLayer('blocklayer');

phaser 似乎可以正确读取 scifi.json 中的'background' 层信息,但不能正确读取'blocklayer' 的信息。

这是 scifi.json 的摘录:

{ "height":20,
 "layers":[
        {
         "compression":"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
        }, 
        {
         "compression":"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,
[...]

我仍然无法找出问题所在...以前有人遇到过吗?

更多信息:

  • 我使用 Atom 作为 IDE

  • 我尝试使用 Phaser v2.0.1 和 Phaser v2.4.2

谢谢。

【问题讨论】:

    标签: javascript json meteor phaser-framework


    【解决方案1】:

    似乎问题来自 Tiled:散列密钥是用 Zlib 压缩的,尽管它根本不应该被压缩,因为移相器还不支持它。

    【讨论】:

    • 要修复该错误,在 Tiled 中转到 Map -> Map properties -> Tile Layer Format 并选择 CSV。然后导出新的 .json 文件。
    【解决方案2】:

    在平铺中,转到地图 -> 地图属性 在那里你会找到平铺层格式。将其更改为 Base64(未压缩),它应该可以工作:)

    【讨论】:

      猜你喜欢
      • 2023-04-02
      • 1970-01-01
      • 2021-05-11
      • 2019-02-19
      • 2019-07-27
      • 2012-03-23
      • 2012-02-12
      • 1970-01-01
      • 2019-06-28
      相关资源
      最近更新 更多