Canvas context used but not updated on canvas
- by John Doe
I am developing an small html5 game, where I have the following code:
if(object.blocks){
var blocks = object.blocks, that = this;
each(blocks,function(index){
that.blocks.push(new Block(this[index]));
});
}
I receive an object with some configuration and instantiate blocks with it. It works fine, but the Block class has an method, called draw:
this.draw = function (ctx){
if(ctx){
var colors = ['#FF0000','#FFFF00','#0000FF','#00FF00'],
color = Math.round(Math.random() * colors.length-1);
ctx.fillStyle = colors[color];
ctx.fillRect(this.x,this.y,this.width,this.height);
}
};
It was working before I moved it into the Block class, but now it draws nothing. This is the code that calls draw:
render: function(ctx){
each(this.blocks,function(index){
this[index].draw(ctx);
});
}
The context comes from the html page, from the main canvas.