i didn't understand why my canvas wasn't drawn. With a little of debugging i discovered that at row 35 the property x of the object "item" returns undefined [ canvasContext.fillRect(item.x, item.y, 5, 5) ]. Someone can explain me why?
var players = [];
players.push(new Player(100, 200));
players.push(new Player(300, 200));
players.push(new Player(400, 400));
players.push(new Player(200, 200));
function Player(x, y) {
this.x = x;
this.y = y;
}
function auto() {
setInterval(
function() {
movePlayers();
updateCanvas();
},
1000
);
}
function movePlayers() {
for (var item in players) {
item.x += 1;
item.y += 1;
}
}
function updateCanvas() {
var canvas = document.getElementById("canvas");
var canvasContext = canvas.getContext("2d");
canvasContext.clearRect(0, 0, canvas.width, canvas.height);
for (var item in players) {
canvasContext.fillRect(item.x, item.y, 5, 5);
}
}
auto()
<canvas id="canvas"></canvas>