I need a logic that, detecting if the value of x, y
has changed, change the position of an object on the map.
I have a map that contains 50x50 tiles
, and in these tiles there are objects that I can move using the code below:
var playerx = 10;
var playery = 15;
function drawMap() {
for (i = 0; i < map.length; i++) {
for (j = 0; j < map[i].length; j++) {
var drawTile = map[i][j];
var drawObj = objectMap[i][j];
var xpos = (i-j) * tileH + mapX;
var ypos = (i+j) * tileH/2 + mapY;
ctx.drawImage(tileImg[drawTile], xpos, ypos);
if (drawObj) {
ctx.drawImage(objectImg[drawObj-1], xpos, ypos - (objectImg[drawObj-1].height));
}
}
}
}
To add the object I use this property:
objectmap[playerx][playery] = 1;
And to remove I use the delete
function:
delete objectmap[playerx][playery];
What happens is that the object is in positions 10 and 15 which are the values of the variables playerx
and playery
, but I have a function that takes the position of the mouse and clicking changes to the value of playerx
and playery
, it happens that as the value of the function changes, it adds objects to the map, without removing the last.
So the map has several objects, but I want the object to be just where the values of the variables point to.
Do you know how to do this with if
or otherwise?