2017-06-05 112 views
0

我正在爲我的主要項目製作一組迷你遊戲,並且正在跟隨互聯網上的一個教程,爲其中的一個製作記憶遊戲。在JQuery中構建內存遊戲迷你遊戲(Javascript)

代碼運行到我生成新板的位置,但代碼的其餘部分似乎不起作用。我在我的智力方面做錯了什麼?誰能告訴我我要去哪裏?

這是我的內存小遊戲代碼。

$(document).ready(function() { 
 

 
    //speech 
 
    var progress = 0; 
 
    var txt; 
 
    $('#complete, #speech').hide(); 
 

 
    function eventHandler() { 
 
    switch (progress) { 
 
     case 0: 
 
     txt = "Complete"; 
 
     break; 
 
     case 1: 
 
     txt = "Move on the the next game"; 
 
     $('#speech').click(function() { 
 
      window.location.href = "minigame4.html"; //next minigame 
 
     }); 
 
     break; 
 
    } 
 
    progress++; 
 
    $('#speech').html(txt); 
 
    } 
 

 
    $('#speech').click(eventHandler); 
 

 
    // Memory Game // 
 

 
    var memory_array = ['A', 'A', 'B', 'B', 'C', 'C', 'D', 'D', 'E', 'E', 'F', 'F', 'G', 'G', 'H', 'H', 'I', 'I', 'J', 'J', 'K', 'K', 'L', 'L']; 
 
    var memory_values = []; 
 
    var memory_tile_ids = []; 
 
    var tiles_flipped = 0; 
 

 
    //shuffle method 
 
    Array.prototype.memory_tile_shuffle = function() { 
 
    var i = this.length, 
 
     j, temp; 
 
    while (--i > 0) { 
 
     j = Math.floor(Math.random() * (i + 1)); 
 
     temp = this[j]; 
 
     this[j] = this[i]; 
 
     this[i] = temp; 
 
    } 
 
    } 
 

 
    //generate new board 
 
    function newBoard() { 
 
    tiles_flipped = 0; 
 
    var output = ''; 
 
    memory_array.memory_tile_shuffle(); 
 
    for (var i = 0; i < memory_array.length; i++) { 
 
     output += '<div id="tile_' + i + '" onclick="memoryFlipTile(this,\'' + memory_array[i] + '\')" class="tiles"></div>'; 
 
    } 
 
    $('#memory_board').html(output); 
 
    } 
 

 
    newBoard(); 
 

 
    // 
 
    // all code works up to here 
 

 
    function memoryFlipTile(tile, val) { 
 
    // When tile is clicked, change colour to white along with its letter 
 
    if (tile.html == "" && memory_values.length < 2) { 
 
     tile.style.background = '#FFF'; 
 
     tile.html = val; 
 
     // If no tiles are flipped 
 
     if (memory_values.length == 0) { 
 
     memory_values.push(val); 
 
     memory_tile_ids.push(tile.id); 
 
     // If one tile is already flipped 
 
     } else if (memory_values.length == 1) { 
 
     memory_values.push(val); 
 
     memory_tile_ids.push(tile.id); 
 
     // See if both tiles are a match 
 
     if (memory_values[0] == memory_values[1]) { 
 
      tiles_flipped += 2; 
 
      // Clear both arrays 
 
      memory_values = []; 
 
      memory_tile_ids = []; 
 
      // Check to see if the whole board is cleared 
 
      // then display complete 
 
      if (tiles_flipped == memory_array.length) { 
 
      $("#complete").show(0, function() { 
 
       eventHandler() 
 
       $('#speech').show(); 
 
      }); 
 
      } 
 
     } else { 
 
      function flip2Back() { 
 
      // Flip the 2 tiles back over 
 
      var tile_1 = document.getElementById(memory_tile_ids[0]); 
 
      var tile_2 = document.getElementById(memory_tile_ids[1]); 
 
      tile_1.style.background - image = 'url(images/puzzle5/blank.png) no-repeat'; 
 
      tile_1.html = ""; 
 
      tile_2.style.background - image = 'url(images/puzzle5/blank.png) no-repeat'; 
 
      tile_2.html = ""; 
 
      // Clear both arrays 
 
      memory_values = []; 
 
      memory_tile_ids = []; 
 
      } 
 
      setTimeout(flip2Back, 700); 
 
     } 
 
     } 
 
    } 
 
    } 
 
});
div#memory_board { 
 
    padding: 24px; 
 
    margin: 0px auto; 
 
    width: 456px; 
 
    height: 300px; 
 
} 
 

 
div#memory_board div { 
 
    float: left; 
 
    margin: 10px; 
 
    padding: 28px; 
 
    font-size: 50px; 
 
    cursor: pointer; 
 
    text-align: center; 
 
    background-image: url(images/puzzle5/blank.png); 
 
} 
 

 
#complete { 
 
    position: absolute; 
 
    width: 105px; 
 
    height: 25px; 
 
    top: 240px; 
 
    left: 289px; 
 
    background-color: black; 
 
    color: white; 
 
    font-size: 20px; 
 
    padding: 10px; 
 
    border-style: solid; 
 
    border-width: 5px; 
 
    border-color: white; 
 
    z-index: 5; 
 
} 
 

 
#speech { 
 
    position: absolute; 
 
    width: 655px; 
 
    height: 100px; \t 
 
    top: 330px; 
 
    left: 15px; 
 
    background-color: black; 
 
    color: white; 
 
    font-size: 25px; 
 
    padding: 10px; 
 
    border-style: solid; 
 
    border-width: 5px; 
 
    border-color: white; 
 
    z-index: 99; 
 
}
<!doctype html> 
 
<html> 
 

 
<head> 
 
    <meta charset="utf-8"> 
 
    <title>MAS340</title> 
 
    <link href="styles.css" rel="stylesheet" /> 
 
    <script src="jquery-3.1.1.min.js"></script> 
 
    <script> 
 
    //javascript goes here 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <div id="stage"> 
 
    <div id="memory_board"></div> 
 
    </div> 
 
</body> 
 

 
</html>

回答

0

除了某些用戶提到的更改外,您的腳本還需要其他更改。由於您已經在使用jQuery,爲什麼不充分利用它?

也有附加事件使用類似下面的jQuery,因爲我懷疑它能夠看到腳本中定義的函數,同時將元素放在dom中。 (雖然還不確定理由)

我已經在下面的小提琴中更改了您的代碼。請檢查其是否適合你的工作(只是糾正語法和使用jQuery代替一些JavaScript代碼)

$(document).on('click','.tiles',function(){ memoryFlipTile($(this),$(this).data("memchar")) })

Check Fiddle

還有優化的代碼範圍。

+0

瓷磚似乎在被點擊時增加了尺寸,我如何使它們尺寸相同? – Ovaflow

+0

這是因爲字體大小設置爲50px是CSS!檢查更新並根據需要調整高度,寬度和填充。 –

0

你要排隊93和95.另外語法錯誤,htmlinnerHTML 變化

tile_1.style.background - image = 'url(images/puzzle5/blank.png) no-repeat'; 

tile_1.html = ""; 

tile_2.style.background - image = 'url(images/puzzle5/blank.png) no-repeat'; 

tile_2.html = ""; 

tile_1.style.['background-image'] = 'url(images/puzzle5/blank.png) no-repeat'; 

tile_1.innerHTML = ""; 

tile_2.style.['background-image'] = 'url(images/puzzle5/blank.png) no-repeat'; 

tile_2.innerHTML = ""; 
+0

沒有'html'屬性 – charlietfl

+0

它應該是innerHTML而不是html –

+0

@charlietfl大聲笑我以爲你是提問者,對不起人。謝謝 –