i'm new to javascript and i'm developing tetris game. i have written some piece of code and reused some code from other sites. But one of the function is causing me some problems and i'm getting an error as "undefined is not an object evaluating board[y][x]". The piece of code is pasted below:
var COLS=10, ROWS=20;
var board = [];
function run() {
ctx.clearRect( 0, 0, canvas.width, canvas.height );
ctx.strokeStyle = 'black';
for ( var x = 0; x < COLS; ++x ) {
for ( var y = 0; y < ROWS; ++y ) {
if ( board[ y ][ x ] ) {
ctx.fillStyle = colors[ board[ y ][ x ] - 1 ];
drawBlock( x, y );
}
}
}
}
i'm not understanding as why am i getting the error. i have tried executing it in both safari and IE 11 but the result is the same. i even tried using the typeof keyword in the if statement but there's no change. Could anyone please help me to get rid of this error.
thanks
[], but you're using it as if it's an array of objects (or arrays). Where is the code that puts values in the "board" array?board[y][x]to beboard[x][y]board[y] && board[y][x]to get at what you expected