I am making a game where I have multiple tile objects stored in an array, and I am trying to call a function "update", to draw the tiles to the screen.
My script:
var canvas = document.getElementById("game canvas");
canvas.width = 600;
canvas.height = 300;
var g = canvas.getContext("2d");
var grass = new Image();
grass.src = "grass.png";
setInterval(draw,1000/60);
function tile(x,y,img) {
this.x = x;
this.y = y;
this.img = img;
this.update = function() {
g.drawImage(this.img,this.x,this.y);
}
}
var maps = {
map1:{
tiles:[
new tile(0,0,grass),
new tile(0,32,grass)
],
update:function() {
for (var i in this.tiles) {
i.update();
}
}
}
};
function draw() {
maps.map1.update();
}
I also tried using an object instead of an array for the tile container, but it didn't work either. It outputs this error into the console:
TypeError: i.update is not a function