這節(jié)內(nèi)容的效果出不來?。?!
不知道哪里敲錯了,隨機數(shù)字沒問題,獲取的number-cell也沒問題,但就是不出來兩個隨機數(shù)字,代碼太多不知道怎么貼,大家有沒有遇到這種情況的?或者有哪些原因會出現(xiàn)這樣的情況?
不知道哪里敲錯了,隨機數(shù)字沒問題,獲取的number-cell也沒問題,但就是不出來兩個隨機數(shù)字,代碼太多不知道怎么貼,大家有沒有遇到這種情況的?或者有哪些原因會出現(xiàn)這樣的情況?
2016-09-03
舉報
2016-09-05
根據(jù)我的經(jīng)驗,一般這種問題都是很簡單的一些錯誤?。?!兩個問題
1.showNumberWithAnimation函數(shù)里取numberCell的時候的$("#number-cell-")要加#號的親
2.還是這個函數(shù)里面的改變背景顏色的css("",getNumberBackgroundColor());后面那個不用加引號啊親。
PS:還有一個問題,雖然對你現(xiàn)在這個問題沒影響,不過也是錯誤的。在updateBoardViwe()函數(shù)里面的那個else判斷里最后沒有加theNumberCell.text(board[i][j]);沒有內(nèi)容樣式改了也是白改呀,親。
不過居然有人同時在學這個課程。。。??丛u論都是2015年的,挫敗感好強
2017-07-20
animation.js里numbercell的backgroundcolor的引號前后不一樣
2016-09-05
那我就貼js里的代碼吧,html和css的應(yīng)該沒問題吧
main2018.js里的代碼
var board = new Array();// 4*4方格里元素的值
var score = 0;
$(document).ready(function () {
?? newGame();
});
function? newGame() {
??? // 初始化棋盤格
??? init();
??? // 在隨機兩個格子里生成數(shù)字
??? generateOneNumber();
??? generateOneNumber();
}
function init() {
??? for(var i=0;i<4;i++){
??????? for(var j=0;j<4;j++){
??????????? var gridCell = $("#grid-cell-"+i+"-"+j);
??????????? // 根據(jù)傳入的坐標來計算距左距上
??????????? gridCell.css('top', getPosTop(i,j));
??????????? gridCell.css('left',getPosLeft(i,j));
??????? }
??? }
??? // board二維數(shù)組
??? for( i = 0; i<4;i++){
??? board[i] = new Array();
??????? for (j=0; j<4; j++){
??????????? board[i][j] = 0;
??????? }
??? }
??? updateBoardView();
}
// 顯示的函數(shù)
function updateBoardView() {
??? // $(".number-cell").remove();
??? for(var i=0; i<4; i++){
??????? for(var j=0; j<4; j++){
??????????? $("#grid-container").append('<div class="number-cell" id="number-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('top',getPosTop(i,j) + 50);
??????????????? theNumberCell.css('left',getPosLeft(i,j) + 50);
??????????? }else{
??????????????? theNumberCell.css('width','100px');
??????????????? theNumberCell.css('height','100px');
??????????????? theNumberCell.css('top',getPosTop(i,j));
??????????????? theNumberCell.css('left',getPosLeft(i,j));
??????????????? theNumberCell.css('background-color',getNumberBackgroundColor(board[i][j]));
??????????????? theNumberCell.css('color',getNumberColor(board[i][j]));
??????????? }
??????? }
??? }
}
// 隨機在一個格子里生成數(shù)字的函數(shù)
function generateOneNumber() {
if(nospace(board)){
??? return false;
}
// 隨機一個位置
??? var randx= parseInt(Math.floor(Math.random() * 4));
??? var randy= parseInt(Math.floor(Math.random() * 4));
??? while (true){
??????? if (board[randx][randy]==0){
??????????? break;
??????? }
??????? randx= parseInt(Math.floor(Math.random() * 4));
??????? randy= parseInt(Math.floor(Math.random() * 4));
??? }
// 隨機一個數(shù)字
??? var randNumber = Math.random()<0.5? 2: 4;
??? // 更新board數(shù)組
board[randx][randy] = randNumber;
??? // 在隨機位置顯示隨機數(shù)字(動畫效果)
??? showNumberWithAnimation(randx,randy,randNumber);
return true;
}
showanimation.js里的代碼
function showNumberWithAnimation(i,j,randNumber) {
??? var numberCell = $("number-cell-"+i+"-"+j);
??? numberCell.css('background-color','getNumberBackgroundColor(randNumber)');
??? numberCell.css("color",getNumberColor(randNumber));
??? numberCell.text(randNumber);
alert(i);
??? alert(j);
??? alert(randNumber);
??? numberCell.animate({
??????? width:"100px",
??????? height:"100px",
??????? top:getPosTop(i,j),
??????? left:getPosLeft(i,j)
??? },50);
}
麻煩大神看一下,如果需要html和css還有support2048里的代碼說一下我再貼
2016-09-03
不貼代碼怎么知道哪里有問題啊