課程
/前端開發
/WebApp
/慕課網2048私人訂制
不知道哪里敲錯了,隨機數字沒問題,獲取的number-cell也沒問題,但就是不出來兩個隨機數字,代碼太多不知道怎么貼,大家有沒有遇到這種情況的?或者有哪些原因會出現這樣的情況?
2016-09-03
源自:慕課網2048私人訂制 2-3
正在回答
根據我的經驗,一般這種問題都是很簡單的一些錯誤?。?!兩個問題
1.showNumberWithAnimation函數里取numberCell的時候的$("#number-cell-")要加#號的親
2.還是這個函數里面的改變背景顏色的css("",getNumberBackgroundColor());后面那個不用加引號啊親。
PS:還有一個問題,雖然對你現在這個問題沒影響,不過也是錯誤的。在updateBoardViwe()函數里面的那個else判斷里最后沒有加theNumberCell.text(board[i][j]);沒有內容樣式改了也是白改呀,親。
不過居然有人同時在學這個課程。。。??丛u論都是2015年的,挫敗感好強
NadiaSmile 提問者
我打點醬油 回復 NadiaSmile 提問者
半生不熟_
animation.js里numbercell的backgroundcolor的引號前后不一樣
那我就貼js里的代碼吧,html和css的應該沒問題吧main2018.js里的代碼
var board = new Array();// 4*4方格里元素的值var score = 0;
$(document).ready(function () {?? newGame();});
function? newGame() {??? // 初始化棋盤格??? init();??? // 在隨機兩個格子里生成數字??? generateOneNumber();??? generateOneNumber();}
function init() {??? for(var i=0;i<4;i++){??????? for(var j=0;j<4;j++){??????????? var gridCell = $("#grid-cell-"+i+"-"+j);
??????????? // 根據傳入的坐標來計算距左距上??????????? gridCell.css('top', getPosTop(i,j));??????????? gridCell.css('left',getPosLeft(i,j));??????? }??? }
??? // board二維數組??? for( i = 0; i<4;i++){??? board[i] = new Array();??????? for (j=0; j<4; j++){??????????? board[i][j] = 0;??????? }??? }??? updateBoardView();}
// 顯示的函數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]));??????????? }??????? }??? }}
// 隨機在一個格子里生成數字的函數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));??? }// 隨機一個數字??? var randNumber = Math.random()<0.5? 2: 4;
??? // 更新board數組board[randx][randy] = randNumber;??? // 在隨機位置顯示隨機數字(動畫效果)??? 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里的代碼說一下我再貼
我打點醬油
不貼代碼怎么知道哪里有問題啊
舉報
慕課網這款“2048私人訂制”通過大神老的講解學習到游戲結構的開發
1 回答為什么打的一模一樣效果出不來
1 回答做完效果出不來,大神幫忙看看
3 回答做完之后執行不出效果
1 回答數字出不來
4 回答代碼和老師一樣的,為什么沒效果,而且越來越大。
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2016-09-05
根據我的經驗,一般這種問題都是很簡單的一些錯誤?。?!兩個問題
1.showNumberWithAnimation函數里取numberCell的時候的$("#number-cell-")要加#號的親
2.還是這個函數里面的改變背景顏色的css("",getNumberBackgroundColor());后面那個不用加引號啊親。
PS:還有一個問題,雖然對你現在這個問題沒影響,不過也是錯誤的。在updateBoardViwe()函數里面的那個else判斷里最后沒有加theNumberCell.text(board[i][j]);沒有內容樣式改了也是白改呀,親。
不過居然有人同時在學這個課程。。。??丛u論都是2015年的,挫敗感好強
2017-07-20
animation.js里numbercell的backgroundcolor的引號前后不一樣
2016-09-05
那我就貼js里的代碼吧,html和css的應該沒問題吧
main2018.js里的代碼
var board = new Array();// 4*4方格里元素的值
var score = 0;
$(document).ready(function () {
?? newGame();
});
function? newGame() {
??? // 初始化棋盤格
??? init();
??? // 在隨機兩個格子里生成數字
??? generateOneNumber();
??? generateOneNumber();
}
function init() {
??? for(var i=0;i<4;i++){
??????? for(var j=0;j<4;j++){
??????????? var gridCell = $("#grid-cell-"+i+"-"+j);
??????????? // 根據傳入的坐標來計算距左距上
??????????? gridCell.css('top', getPosTop(i,j));
??????????? gridCell.css('left',getPosLeft(i,j));
??????? }
??? }
??? // board二維數組
??? for( i = 0; i<4;i++){
??? board[i] = new Array();
??????? for (j=0; j<4; j++){
??????????? board[i][j] = 0;
??????? }
??? }
??? updateBoardView();
}
// 顯示的函數
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]));
??????????? }
??????? }
??? }
}
// 隨機在一個格子里生成數字的函數
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));
??? }
// 隨機一個數字
??? var randNumber = Math.random()<0.5? 2: 4;
??? // 更新board數組
board[randx][randy] = randNumber;
??? // 在隨機位置顯示隨機數字(動畫效果)
??? 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
不貼代碼怎么知道哪里有問題啊