JS 没有运行 do-while 循环

时间:2021-03-27 14:16:29

标签: javascript loops do-while

我正在尝试制作一个小型 js 游戏,但我不知道这里出了什么问题。

它不会等待用户的点击,而是一直运行到最后并给出 gameOver() 函数的警报。

为什么不等待?为什么在用户点击容器元素的 lastChild 之前它不循环?

/************************************************
 * Positioning function
 ************************************************/
function gamePostitioning () {
    var x = Math.floor((Math.random() * 350 ));
    var y = Math.floor((Math.random() * 350 ));
    return {x, y};
 };

/************************************************
 * LeftSide smiling faces generator
 ************************************************/
var numberOfFaces = 0;
var theLeftSide = document.getElementById('leftSide');

function generateFaces() {
    theLeftSide.innerHTML = "";

    for (var i = 0; i < numberOfFaces; i++) {
        
        var imgElement = document.createElement('IMG');

        imgElement.src = "smile.png";
        imgElement.style.top = gamePostitioning().y + "px";
        imgElement.style.left = gamePostitioning().x + "px";

        theLeftSide.appendChild(imgElement);
    }
    /* cheating */
    console.log(theLeftSide.lastChild);
 };
 
/************************************************
 * Cloning the left-1 to RightSide
 ************************************************/
  var theRightSide = document.getElementById('rightSide');

  function cloneGeneration() {
  
      theRightSide.removeChild(theRightSide.firstChild)
      var cloned = theLeftSide.cloneNode(true);
      cloned.id =""; 
      theRightSide.appendChild(cloned).removeChild(cloned.lastChild);
 };

/************************************************
 * GAME OVER function + local store the best result
 ************************************************/
function gameOver() {
    var current = numberOfFaces/5

    if( !best || best < current){
        best = localStorage.setItem('best', current);
    }else{
        best = localStorage.getItem('best');
    }

    alert(
        'Game OVER!\n\n' +
        'Current level: ' + current + '. level\n\n' +
        'Your best result: ' + best + '. level'
        );
 }
 
numberOfFaces += 5;
generateFaces(),
cloneGeneration();


do {
    theLeftSide.lastChild.onclick = function toNextLevel(event){ 
        event.stopPropagation();
        numberOfFaces += 5;

        generateFaces();
        cloneGeneration();
    }
} while ( theLeftSide.lastChild.onclick ) 

gameOver();
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>.JS Matching Game</title>
        <meta charset="UTF-8">
        <meta name="viewport" content="width=device-width, initial-scale=1.0">

        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
        <link rel="stylesheet" href="style.css">

    </head>
    <body>
        <div class="container">
            <div class="row">
                <div class="col-sm-12" id="header">
                    <h2> Matching Game</h2>
                    <p>Click on the extra smiling face in the white box!</p>
                </div>
            </div>

            <div class="row" id="gameSpace">
                <div class="col-xs-12">
                    <div id="leftSide">
                    </div> 
                </div>
            </div>
            
            <div class="row" id="gameSpace">
                <div class="col-xs-12">
                    <div id="rightSide">
                    </div>
                </div>
            </div>
        </div>
    
    <script src="script.js"></script>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

gameOver 运行在脚本的末尾,所以它自然会结束游戏。您需要一个条件来查看游戏是否已结束。您的目的是让用户在游戏结束前获得 X 次点击吗?在那种情况下,我会有点不同。

此外,您不需要循环事件传播。它会在事件发生时触发。我在代码中做了一些更改,但还必须创建两个 TEMP 存储方法,因为 Stackoverflow 不允许本地存储。

我在事件方法和 gameOver 中进行了更改。游戏似乎还没有完成,但我在代码中标记了我更改的所有内容。您在使用 best 中的变量 gameOver 时遇到了问题,我已更正。

/************************************************
 * Positioning function
 ************************************************/
function gamePostitioning() {
  var x = Math.floor((Math.random() * 350));
  var y = Math.floor((Math.random() * 350));
  return {
    x,
    y
  };
};

/************************************************
 * LeftSide smiling faces generator
 ************************************************/
var numberOfFaces = 0;
var theLeftSide = document.getElementById('leftSide');

function generateFaces() {
  theLeftSide.innerHTML = "";

  for (var i = 0; i < numberOfFaces; i++) {

    var imgElement = document.createElement('IMG');

    imgElement.src = "smile.png";
    imgElement.style.top = gamePostitioning().y + "px";
    imgElement.style.left = gamePostitioning().x + "px";

    theLeftSide.appendChild(imgElement);
  }
  /* cheating */
  console.log(theLeftSide.lastChild);
};

/************************************************
 * Cloning the left-1 to RightSide
 ************************************************/
var theRightSide = document.getElementById('rightSide');

function cloneGeneration() {

  theRightSide.removeChild(theRightSide.firstChild)
  var cloned = theLeftSide.cloneNode(true);
  cloned.id = "";
  theRightSide.appendChild(cloned).removeChild(cloned.lastChild);
};

/************************************************
 * GAME OVER function + local store the best result
 ************************************************/
function gameOver() {
  var current = numberOfFaces / 5

  // DECLARE 'best' as a variable, and read it directly from localStorage
  var best = getBestFromLocalStorageTemp() || 0;

  if (!best || best < current) {
    storeBestFromLocalStorageTemp(current);
  }

  alert(
    'Game OVER!\n\n' +
    'Current level: ' + current + '. level\n\n' +
    'Your best result: ' + best + '. level'
  );
}

var storageBestTemp;

function getBestFromLocalStorageTemp() {
  return storageBestTemp;
//  return localStorage.getItem('best')
}

function storeBestInLocalStorageTemp(value) {
  storageBestTemp = value;
  // return localStorage.setItem('best', value);
}

numberOfFaces += 5;
generateFaces();
cloneGeneration();


// do {
theLeftSide.lastChild.onclick = function toNextLevel(event) {
    // event.stopPropagation();
    numberOfFaces += 5;

    generateFaces();
    cloneGeneration();
    
    let CONDITION = true;
    
    // I MOVED THIS INSIDE THIS LISTENER
    // NEEDS A CONDITION TO NOT ACTIVATE
    if (CONDITION) {
      gameOver();
    }
}
    // } while ( theLeftSide.lastChild.onclick ) 
<!DOCTYPE html>
<html lang="en">

<head>
  <title>.JS Matching Game</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">

  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">
  <link rel="stylesheet" href="style.css">

</head>

<body>
  <div class="container">
    <div class="row">
      <div class="col-sm-12" id="header">
        <h2> Matching Game</h2>
        <p>Click on the extra smiling face in the white box!</p>
      </div>
    </div>

    <div class="row" id="gameSpace">
      <div class="col-xs-12">
        <div id="leftSide">
        </div>
      </div>
    </div>

    <div class="row" id="gameSpace">
      <div class="col-xs-12">
        <div id="rightSide">
        </div>
      </div>
    </div>
  </div>

  <script src="script.js"></script>
</body>

</html>