var?chessBoard?=?[];
var?me?=?true;
for(var?i?=?0;?i<15;?i++)?{
chessBoard[i]?=?[];
for(var?j=0;?j<15;?j++)?{
chessBoard[i][j]?=?0;
}
}
var?chess?=?document.getElementById('chess');
var?context?=?chess.getContext('2d');
context.strokeStyle=?"#BFBFBF";
var?logo?=?new?Image();
logo.src?=?"images/logo.png";
logo.onload?=?function(){
context.drawImage(logo,?0,0,450,450);
drawChessBoard();
}
var?drawChessBoard?=?function(){
for(var?i?=?0;?i?<?15?;?i++){
context.moveTo(?15+30*i,?15);
context.lineTo(15+30*i,?435);
context.stroke();
context.moveTo(15,?15+30*i);
context.lineTo(435,?15+?30*i);
context.stroke();
}
}
var?oneStep?=?function(i,?j,?me){
context.beginPath();
context.arc(15?+?i*30,?15?+?j*30,?13,?0,?2*Math.PI);
context.closePath();
var?gradient?=?context.createRadialGradient(15?+?i*30?+?2,?15?+?j*30?-?2,?13,?15?+?i*30?+?2,?15?+?j*30?-?2,0);
if(me)?{
gradient.addColorStop(0,?"#0A0A0A");
gradient.addColorStop(1,"#636766");
}else{
gradient.addColorStop(0,?"#D1D1D1");
gradient.addColorStop(1,?"#F9F9F9");
}
context.fillStyle?=?gradient;
context.fill();
}
chess.onclick?=?function(e)?{
var?x?=?e.offsetx;
var?y?=?e.offsetY;
var?i?=?Math.floor(x?/?30);
var?j?=?Math.floor(y?/?30);
if(chessBoard[i][j]?==?0)?{
oneStep(i,?j,?me);
if(me)?{
chessBoard[i][j]?=?1;
}else{
chessBoard[i][j]?=?2;
}
me?=?!me;
}
}
當(dāng)我點擊棋盤左上角時,報錯:Uncaught?TypeError:?Cannot?read?property?'0'?of?undefined
點擊第二條橫線,則報錯:Uncaught?TypeError:?Cannot?read?property?'1'?of?undefined?
麻煩老師幫我看一下。謝謝哈^_^?!!
2016-03-27
45行,offsetX
2016-12-06
57 前面的y換成j
2016-07-27
//我的調(diào)試的時候也沒有報錯,就是落子點擊沒反應(yīng),路過的大神能幫忙看看嗎?
2016-07-18
= =。我把你的復(fù)制了一遍,結(jié)果跟我原來打的一樣沒圖片沒棋盤 求解
2016-05-06
代碼一樣,不知道為什么棋盤都沒了。。。
2016-03-31
你這代碼怎么上傳d 呢?