如何从像素数组中绘制地图

时间:2014-04-23 15:29:01

标签: javascript html5

我正在尝试从图像加载地图,我得到了图像上每个像素的十六进制代码,这些代码被放入map []中。 world []将在spritesheet上存储1或0或精灵数量的任何数字。如果颜色是#8aff00那么它将为草存储0。地图是16 x 16像素,这是我的世界大小。当我尝试创建world数组时,for语句正常工作,没有错误。我知道我有所有的数据,但我的这部分功能不起作用,它只是在这些语句之后停止:

    function createWorld() {
       for (var y=0; y < worldHeight; y++) {
           for (var x=0; x < worldWidth; x++) {
             if (map[pl] == '#8aff00') world[x][y] = 0;
             if (map[pl] == '#000000') world[x][y] = 1;
             pl+=4;
          }
       }
       alert('about to draw');
       draw();

   }

警报永远不会被调用。当我显示x和y时,它变为'0,0'变为'1,0'然后变回'0,0'表示值x和y。 pl是我想要的数组的数量。当我把另一个声明处理时,它搞砸了更多。代码是否有问题,如果您需要更多代码,请告诉我。

1 个答案:

答案 0 :(得分:0)

&#39; ave设法构建正在运行的代码。

现在轮到您根据需要实施它了。

worldHeight = 16;
worldWidth = 16;
world = {};
map = ['#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000','#8aff00','#000000']
pl = 0

function draw(){
    console.log(world);
}

function createWorld() {
       for (var y=0; y < worldHeight; y++) {
           for (var x=0; x < worldWidth; x++) {
             if (map[pl] == '#8aff00')
                 world[x] = 0;
                 world[x][y] = 0;
             if (map[pl] == '#000000')
                 world[x] = 0;
                 world[x][y] = 1;
             pl+=4;
          }
       }
       alert('about to draw');
       draw();

}

createWorld();

干杯。