0

我正在编写一些 javascript 来从数组创建 x 和 y 坐标,所以我有以下代码:

var map1 = [
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
    gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,gg,
];
  for (var i=0; i<map1.length; i++) {
    if (i > 16) {
        dy = Math.floor(i / 15) * 16;
    }
    dx = i * 16
    while (dx >= 240) {
        dx = dx - 240;
    }   
  }

并且代码工作得非常好,除了坐标 0,1 和 1,1 没有工作?这些坐标正在使用此处页面上的主体构建具有 16px 块的画布。并且适合这个等式,dx谢谢!dyctx.drawImage(tileset, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight);

4

1 回答 1

0

不确定这是否有帮助 - 但控制台中的以下代码段似乎为所有位置提供了正确的结果。您能否解释一下您期望的输出与您得到的实际输出?

var map1 = new Array(225);
var dx, dy;

for (var i=0; i<map1.length; i++) {
    if (i > 16) {
       dy = Math.floor(i / 15) * 16;
    }
    dx = i * 16
    while (dx >= 240) {
        dx = dx - 240;
    }
    console.log(dx + ',' + dy); //Seems to print all positions ok
}
于 2013-05-06T01:26:34.223 回答