Question

I am writing some javascript to create x and y coodinates from an array, so i have this code:

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;
    }   
  }

And the code works extremely well, except for coordinates 0,1 and 1,1 arent working? These coordinates are building a canvas with 16px blocks using the principal on this page here. And dx and dy fit into this equation, ctx.drawImage(tileset, sx, sy, sWidth, sHeight, dx, dy, dWidth, dHeight); Thanks!

Was it helpful?

Solution

Not sure if this helps - but the following snippet in the console seems to give the correct results for all positions. Can you perhaps explain what output you're expecting vs the actual output you're getting ?

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
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top