为啥出不来两个随机数呀,代码对了挺多遍感觉没有问题呀,有没有大神帮忙看看,
main.js
var board=new Array();
var score=0;
$(document).ready(function(e){
newgame();
});
function newgame(){
//初始化
init();
//在随机的两个格子里生成数字
generateNumber();
generateNumber();
}
function init(){
for(var i=0;i<4;i++)
{
for(var j=0;j<4;j++)
{
var cell=$('#cell-'+i+'-'+j);
cell.css("top",gettop(i,j));
cell.css("left",getleft(i,j));
}
}
for(var i=0;i<4;i++)
{
board[i]=new Array();
for(var j=0;j<4;j++)
board[i][j]=0;
}
updateboard();
}
function updateboard(){
//$("numbur-cell").remove();
for (var i = 0; i <4; i++) {
for(var j=0;j<4;j++){
$("#container").append(
'<div class="numbur-cell" id=" numbur-cell- '+i+' - '+j+' "> </div>'
);
var theNumbercell=$('#number-cell-'+i+'-'+j);
if (board[i][j]==0) {
theNumbercell.css('width','0px');
theNumbercell.css('height','0px');
theNumbercell.css('left',getleft(i,j)+50);
theNumbercell.css('top',gettop(i,j)+50);
}
else{
theNumbercell.css('width','100px');
theNumbercell.css('height','100px');
theNumbercell.css('left',getleft(i,j));
theNumbercell.css('top',gettop(i,j));
theNumbercell.css('background-color',getNumberBKcolor(board[i][j]));
theNumbercell.css('color',getNumbercolor(board[i][j]));
theNumbercell.text(board[i][j]);
}
}
}
}
function generateNumber(){
if (nospace(board))
return false;
//确定位置
var x= parseInt(Math.floor(Math.random() * 4));
var y= parseInt(Math.floor(Math.random() * 4));
while (true){
if (board[x][y]==0){
break;
}
x= parseInt(Math.floor(Math.random() * 4));
y= parseInt(Math.floor(Math.random() * 4));
}
//确定数字
var Rnumber=Math.random()<0.5?2:4;
//显示数字
board[x][y]=Rnumber;
showNumber(x,y,Rnumber);
return true;
}
animate.js
function showNumber(i,j,Rnumber){
var Numbercell=$('#number-cell-'+i+'-'+j);
Numbercell.css("background-color",getNumberBKcolor(Rnumber));
Numbercell.css("color",getNumbercolor(Rnumber));
Numbercell.text(Rnumber);
Numbercell.animate({
width:"100px",
height:"100px",
left:getleft(i,j),
top:gettop(i,j)
},50);
}
检查过,没有报错