我基本上尝试使用Phaser创建平台游戏。我在Tiled中创建了一个JSON文件,现在我正在尝试将它添加到世界中。我实际上是在过去的三个小时内尝试添加它,但没有成功。我查看了许多帖子和教程,但没有任何效果。希望有人可以提供帮助吗?
这是我的HTML:
<!DOCTYPE html><html>
<head>
<title>Platformer</title>
<script type="text/javascript" src="phaser.js"></script>
<script type="text/javascript" src="Game.js"></script>
</head>
<body>
<div id="gameDiv"></div>
</body>
我的Game.js:
var game = new Phaser.Game(800,600,Phaser.AUTO,"gameDiv",{ preload: preload, create: create, update: update });
function preload() {
game.load.tilemap("map","assets/LvlOneMap.json",null,Phaser.Tilemap.TILED_JSON);
game.load.image("tiles","assets/spritesheet_2.png");
}
var map;
var layer;
function create(){
map = game.add.tilemap("map");
map.addTilesetImage("Tiles","tiles");
layer = map.createLayer("GroundLayer");
layer.resizeWorld();
}
function update() {
//Gameloop
}
这是我的&#34; LvlOneMap.json&#34;文件:
{ "height":10,
"layers":[
{
"data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 137, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 138, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 0, 0, 0, 0, 123, 123, 123, 123, 123, 123, 123, 123, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 0, 0, 0, 0, 153, 153, 155, 155, 155, 155, 155, 155, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 122, 122, 0, 0, 153, 153, 155, 155, 155, 155, 155, 155, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 153, 155, 155, 155, 155, 155, 155, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 153, 153, 155, 155, 155, 155, 155, 155, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 123, 123, 123, 123, 123, 123, 123, 153, 153, 155, 155, 155, 155, 155, 155, 0, 0, 0, 0, 0, 0, 123, 123, 123, 123, 123, 123, 153, 153, 153, 153, 153, 153, 153, 153, 155, 155, 155, 155, 155, 155, 155, 123, 123, 123, 123, 123, 123, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 153, 155, 155, 155, 155, 155],
"height":10,
"name":"BackgroundLayer",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":27,
"x":0,
"y":0
},
{
"data":[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
"height":10,
"name":"GroundLayer",
"opacity":1,
"type":"tilelayer",
"visible":true,
"width":27,
"x":0,
"y":0
}],
"nextobjectid":1,
"orientation":"orthogonal",
"properties":
{
},
"renderorder":"right-down",
"tileheight":21,
"tilesets":[
{
"firstgid":1,
"image":"spritesheet_2.png",
"imageheight":372,
"imagewidth":694,
"margin":3,
"name":"Tiles",
"properties":
{
},
"spacing":2,
"tilecount":480,
"tileheight":21,
"tilewidth":21,
"transparentcolor":"#4a698c"
}],
"tilewidth":21,
"version":1,
"width":27
}
我还应该注意,图层的名称与Tiled中的名称相匹配,并且图块层格式设置为CSV。
答案 0 :(得分:2)
如果我正确地读取JSON,GroundLayer
中没有任何对象(数据对象的全部为0)。
您的代码与我将其进行比较的工作示例相匹配,因此我建议您验证是否保存了图层数据。
尝试将layer = map.createLayer("GroundLayer");
切换为layer = map.createLayer("BackgroundLayer");
以查看图层是否显示,因为图层中显示的是实际数据。