我从图像中获取像素数组并将其存储到map []中 然后它将测试十六进制代码中的颜色是什么,然后返回精灵的指定数字 现在0为草,1为人行道。
第一部分工作正常但是当我进入draw()时,world [] var返回undefined,但是在for语句的createWorld中它返回0或1.
这就是我所拥有的:
function createWorld() {
for (var y = 0; y < worldHeight; y++) {
for (var x = 0; x < worldWidth; x++) {
if (map[pl] == '#8aff00') {
world[x] = [];
world[x][y] = 0;
/*alert(x + ', ' + y + ', ' +'green' + ', '
+ map[pl] + ', ' + world[x] + ',' + world[x][y]);*/
}
if (map[pl] == '#000000') {
world[x] = [];
world[x][y] = 1;
/*alert(x + ', ' + y + ', ' +'black' + ', '
+ map[pl] + ', ' + world[x][y]);*/
}
pl += 4;
}
}
draw();
}
function draw() {
if (!spritesheetLoaded) return;
ctx.fillStyle = '#000000';
ctx.fillRect(0, 0, canvas.width, canvas.height);
for (var y = 0; y < worldHeight; y++) {
for (var x = 0; x < worldWidth; x++) {
switch (world[x][y]) {
case 1:
ctx.drawImage(spritesheet,
sidewalk.srcX * tileWidth, sidewalk.srcY * tileHeight,
tileWidth, tileHeight, x * tileWidth, y * tileHeight,
tileWidth, tileHeight);
break;
default:
ctx.drawImage(spritesheet,
grass.srcX * tileWidth, grass.srcY * tileHeight,
tileWidth, tileHeight, x * tileWidth, y * tileHeight,
tileWidth, tileHeight);
break;
}
}
}
}