2017-04-17 95 views
0

我正在学习JS,我正在制作一个游戏,我必须找到入侵者。 我的配对游戏无法正常工作,因为左侧的面孔必须增加5个,因为我的代码增加了10?我的配对游戏无法正常工作

<!DOCTYPE html> 
 
<html> 
 
<head> 
 
    <title>Matching Game</title> 
 
\t <meta name="author" content="Aurora Ruggieri"> 
 
    
 
\t <style> 
 
\t img{ 
 
\t  position: absolute; 
 
\t  height: 100px; 
 
\t \t width: 100px; 
 
\t } 
 
\t 
 
\t div{ 
 
\t  position: absolute; 
 
\t \t width: 500px; 
 
\t \t height: 500px; 
 
    } 
 
\t 
 
\t #rightSide { 
 
\t  left: 500px; 
 
     border-left: 1px solid black; 
 
\t } 
 
\t </style> 
 
</head> 
 
<body onload="generateFaces()"> 
 
    <h1>Matching Game</h1> 
 
    <p>Click on the extra smiling face on the left</p> 
 
    
 
    <div id="leftSide"></div> 
 
    <div id="rightSide"></div> 
 
    
 
    <script> 
 
    var numberOfFaces = 5; 
 
    var theLeftSide = document.getElementById("leftSide"); 
 
\t var theRightSide = document.getElementById("rightSide"); 
 
\t 
 
    
 
\t function generateFaces(){ 
 
\t for (i= 0; i < numberOfFaces; i++) { 
 
\t  var top_position= Math.floor(Math.random() * 401); 
 
\t  var left_position= Math.floor(Math.random() * 401); 
 
\t \t var leftSideImage = document.createElement("img"); 
 
     \t leftSideImage.setAttribute("src", "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png"); 
 
\t \t leftSideImage.style.top = top_position + "px"; 
 
     leftSideImage.style.left = left_position + "px"; 
 
\t  theLeftSide.appendChild(leftSideImage); 
 
\t \t var leftImages = theLeftSide.cloneNode(true); 
 
\t \t leftImages.removeChild(leftImages.lastChild); 
 
\t \t theRightSide.appendChild(leftImages); 
 
\t } 
 
     var theBody = document.getElementsByTagName("body")[0]; 
 
\t  
 
\t \t theLeftSide.lastChild.onclick = function nextLevel(event){ 
 
     event.stopPropagation(); 
 
     while(theLeftSide.firstchild) { 
 
\t \t theLeftSide.removeChild(theLeftSide.firstchild); 
 
\t \t } 
 
\t \t while(theRightSide.firstchild) { 
 
\t \t theRightSide.removeChild(theRightSide.firstchild); 
 
\t \t } 
 
\t \t numberOfFaces += 5; 
 
     generateFaces(); 
 
    }; 
 
\t 
 
\t  theBody.onclick = function gameOver() { 
 
     alert("Game Over!"); 
 
     theBody.onclick = null; 
 
     theLeftSide.lastChild.onclick = null; 
 
    }; 
 
    } 
 
\t </script> 
 
</body> 
 
</html>

在此先感谢

回答

0

凯尔·理查森,感谢您的答复。游戏必须增加5面孔,每当你发现入侵者,下一轮将增加5个,然后再增加5个...

+0

如果你采取我的建议并删除该行代码。这正是它的工作原理。 –

+0

我刚刚在我的上面的答案中提供了一个片段,以便您可以看到它的工作。 –

2

如果你想添加5个面每一轮,删除下面的代码行:

numberOfFaces += 5; 

它是如何工作目前是,第一轮有5个面孔,下一轮将增加10个,然后再增加15个,然后再增加20个,等等。

运行这个提供的代码片段,你会发现删除那一行代码可以使它工作。

var numberOfFaces = 5; 
 
    var theLeftSide = document.getElementById("leftSide"); 
 
\t var theRightSide = document.getElementById("rightSide"); 
 
\t 
 
    
 
\t function generateFaces(){ 
 
\t for (i= 0; i < numberOfFaces; i++) { 
 
\t  var top_position= Math.floor(Math.random() * 401); 
 
\t  var left_position= Math.floor(Math.random() * 401); 
 
\t \t var leftSideImage = document.createElement("img"); 
 
     \t leftSideImage.setAttribute("src", "http://home.cse.ust.hk/~rossiter/mooc/matching_game/smile.png"); 
 
\t \t leftSideImage.style.top = top_position + "px"; 
 
     leftSideImage.style.left = left_position + "px"; 
 
\t  theLeftSide.appendChild(leftSideImage); 
 
\t \t var leftImages = theLeftSide.cloneNode(true); 
 
\t \t leftImages.removeChild(leftImages.lastChild); 
 
\t \t theRightSide.appendChild(leftImages); 
 
\t } 
 
     var theBody = document.getElementsByTagName("body")[0]; 
 
\t  
 
\t \t theLeftSide.lastChild.onclick = function nextLevel(event){ 
 
     event.stopPropagation(); 
 
     while(theLeftSide.firstchild) { 
 
\t \t theLeftSide.removeChild(theLeftSide.firstchild); 
 
\t \t } 
 
\t \t while(theRightSide.firstchild) { 
 
\t \t theRightSide.removeChild(theRightSide.firstchild); 
 
\t \t } 
 
     generateFaces(); 
 
    }; 
 
\t 
 
\t  theBody.onclick = function gameOver() { 
 
     alert("Game Over!"); 
 
     theBody.onclick = null; 
 
     theLeftSide.lastChild.onclick = null; 
 
    }; 
 
    } 
 
    
 
    generateFaces();
\t img{ 
 
\t  position: absolute; 
 
\t  height: 100px; 
 
\t \t width: 100px; 
 
\t } 
 
\t 
 
\t div{ 
 
\t  position: absolute; 
 
\t \t width: 500px; 
 
\t \t height: 500px; 
 
    } 
 
\t 
 
\t #rightSide { 
 
\t  left: 500px; 
 
     border-left: 1px solid black; 
 
\t }
<h1>Matching Game</h1> 
 
    <p>Click on the extra smiling face on the left</p> 
 
    
 
    <div id="leftSide"></div> 
 
    <div id="rightSide"></div> 
 

+0

如果我的答案解决了你的问题,你会把它标记为接受请 –