Canvas context used but not updated on canvas

Posted by John Doe on Stack Overflow See other posts from Stack Overflow or by John Doe
Published on 2013-07-01T23:03:06Z Indexed on 2013/07/01 23:05 UTC
Read the original article Hit count: 477

Filed under:
|

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.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about canvas