绘制2d分层地图javascript

时间:2013-03-24 17:37:04

标签: javascript 2d layer tiles

我在javascript中使用canvas建立一个2d rpg游戏,有点像browserquest。

我知道如何绘制一个简单的二维数组来显示画布的单个图块平铺图。

但是为了在玩家移动时获得更好的效果,我希望能够在Tiled中创建分层的平铺图。我在想这些层:

背景层保存地面信息和草地,岩石等

播放器/对象层,用于保存播放器所在位置以及对象所在位置的信息。

碰撞图层,用于保存用户将不会碰撞的位置。

前景图层,用于存放玩家可以移动到的地方,即。树顶和屋顶等。

这听起来不错吗?

我的问题:

如何从平铺地图编辑器加载保存为JSON的地图数据? 然后我如何使用这些信息在画布上显示它?

另外,我如何从我的播放器级别查看有关图层的详细信息?例如,当他们在不同的层时,我将如何检查碰撞?

提前感谢您提供任何帮助

汤姆

编辑:我使用TILED创建了一个json tilemap:

{ "height":15,
 "layers":[
    {
     "data":[79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 28, 29, 30, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 28, 29, 30, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 28, 29, 30, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 28, 29, 30, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 12, 13, 13, 13, 13, 111, 29, 30, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 28, 29, 29, 29, 29, 29, 29, 30, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 28, 29, 128, 45, 45, 45, 45, 46, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 28, 29, 30, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 28, 29, 30, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 12, 13, 13, 13, 13, 13, 111, 29, 112, 13, 13, 13, 13, 13, 13, 13, 13, 13, 13, 79, 28, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 29, 79, 28, 29, 128, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 45, 79, 28, 29, 30, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 28, 29, 30, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 28, 29, 30, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79, 79],
     "height":15,
     "name":"background",
     "opacity":1,
     "type":"tilelayer",
     "visible":true,
     "width":20,
     "x":0,
     "y":0
    }, 
    {
     "data":[0, 0, 0, 0, 0, 149, 150, 151, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 165, 166, 167, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 168, 0, 0, 0, 181, 182, 183, 0, 0, 0, 0, 0, 0, 0, 0, 168, 0, 0, 0, 0, 0, 0, 0, 222, 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, 237, 0, 0, 0, 245, 246, 247, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 152, 0, 0, 0, 168, 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, 152, 222, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 152, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 152, 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, 168, 0, 0, 0, 0, 0, 0, 0, 0, 0, 149, 150, 151, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 165, 166, 167, 0, 0, 0, 152, 0, 237, 0, 0, 0, 0, 0, 0, 245, 246, 247, 0, 168, 181, 182, 183],
     "height":15,
     "name":"background_objects",
     "opacity":1,
     "type":"tilelayer",
     "visible":true,
     "width":20,
     "x":0,
     "y":0
    }, 
    {
     "height":15,
     "name":"collisions",
     "objects":[
            {
             "height":32,
             "name":"",
             "properties":
                {

                },
             "type":"",
             "visible":true,
             "width":100,
             "x":-3,
             "y":192
            }, 
            {
             "height":31,
             "name":"",
             "properties":
                {

                },
             "type":"",
             "visible":true,
             "width":32,
             "x":159,
             "y":448
            }, 
            {
             "height":30,
             "name":"",
             "properties":
                {

                },
             "type":"",
             "visible":true,
             "width":94,
             "x":385,
             "y":448
            }, 
            {
             "height":31,
             "name":"",
             "properties":
                {

                },
             "type":"",
             "visible":true,
             "width":33,
             "x":511,
             "y":160
            }, 
            {
             "height":27,
             "name":"",
             "properties":
                {

                },
             "type":"",
             "visible":true,
             "width":32,
             "x":320,
             "y":258
            }, 
            {
             "height":30,
             "name":"",
             "properties":
                {

                },
             "type":"",
             "visible":true,
             "width":31,
             "x":128,
             "y":97
            }],
     "opacity":1,
     "type":"objectgroup",
     "visible":true,
     "width":20,
     "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, 206, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 197, 198, 199, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 213, 214, 215, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 221, 0, 0, 0, 229, 230, 231, 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, 206, 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, 197, 198, 199, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 213, 214, 215, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 221, 0, 0, 0, 0, 0, 0, 229, 230, 231, 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":15,
     "name":"foreground",
     "opacity":1,
     "type":"tilelayer",
     "visible":true,
     "width":20,
     "x":0,
     "y":0
    }],
 "orientation":"orthogonal",
 "properties":
{

},
 "tileheight":32,
 "tilesets":[
    {
     "firstgid":1,
     "image":"..\/..\/..\/..\/..\/..\/..\/xampp\/htdocs\/DIS\/images\/mountain_landscape_23.png",
     "imageheight":512,
     "imagewidth":512,
     "margin":0,
     "name":"level_1",
     "properties":
        {

        },
     "spacing":0,
     "tileheight":32,
     "tilewidth":32
    }],
 "tilewidth":32,
 "version":1,
 "width":20
 }

2 个答案:

答案 0 :(得分:1)

javascript框架“Melon JS”可以导入Tiled TMX地图。最好使用类似的东西(而不是重新发明轮子)。这是一个链接:

http://www.melonjs.org/

答案 1 :(得分:1)

@Chris Nash感谢您的建议!

我找到了一个关于使用平铺瓷砖地图加载的教程,我认为我将遵循平铺。

万一有人Json loader for tiled map editor