I am trying to initialise an array of Image() objects, with each having a different x position. However all the objects in the array seems to end up with the same x position. I have tried searching for a solution and it seems that it is related to 'closure' however I am not sure how to fix it.
This is my code:
function initPieces(){
  for (var i = 0; i<8; i++) {
    var piece = new Image();
    piece.x = 5 + i*50;
    piece.y = 5;
    piece.src = "images/piece.png";
    piecesArray.push(piece);
    alert(i + ", " + piecesArray[i].x);
  }
}
I have even tried initialising the piecesArray without a loop by manually declaring a new Image() and setting the x position manually for each of the 8 pieces. However it gives me  the same problem.
Anyone's help finding a solution will be greatly appreciated.