代码之家  ›  专栏  ›  技术社区  ›  Noah Leuthold

为什么事件侦听器中的变量更改不会影响函数中的if语句

  •  -2
  • Noah Leuthold  · 技术社区  · 3 年前

    我正在尝试用JavaScript、HTML和CSS制作Simon color游戏的副本。实际游戏的彩色按钮都是SVG路径,因此具有事件侦听器,因此它们可以充当按钮。我还有两个基本的HTML按钮,一个用来启动游戏,另一个用来重置它。我希望在按下开始按钮之前禁用侦听器,然后在按下重置按钮时再次禁用。

    以下是我的代码链接: https://jsfiddle.net/NoahLeuthold/xbm63hvu/7/

    通过做一些谷歌搜索,我猜这与范围有关,但我找不到一种方法来让任何东西发挥作用。

    我尝试将所有内容(包括事件侦听器)放入 play() 播放() 功能,放置“ 播放() 播放() 函数,而不是“ 播放() ".

    HTML:

    <!DOCTYPE html>
    <html>
        <head>
            <title>Simon Game</title>
            <link rel="stylesheet" href="style.css" />
        </head>
        <body>
            <div id="board">
                <div id="scoreboard">
                    Score:
                    <br>
                    <p id="score">0</p>
                </div>
    
                <button id="start">START</button>
                <button id="reset">RESET</button>
    
                <div id="rules">
                    <p id="rulesTitle">Rules</p>
                    The rules are simple, memorize the pattern shown and repeat it. Every round, the pattern gets longer. If you mess up even once, the game ends.
                    <p id="goodluck">Good luck!</p>
                </div>
    
                <div id="game">
                    <div id="blackCircle">
                        <div id="centerCircle"></div>
    
    
                        <svg width="100%" height="100%" viewBox="0 0 100 100" preserveAspectRatio="none">
                            <path id="yellow" d="
                            M 5 47.5 
                            Q 10 10, 47.5 5 
                            v 22.5
                            Q 30 30, 27.5 47.5
                            Z
                            " 
                            stroke="darkgray" fill="yellow"/>
    
                            <path id="blue" d="
                            M 52.5 5 
                            Q 90 10, 95 47.5 
                            h -22.5
                            Q 70 30, 52.5 27.5
                            Z
                            " 
                            stroke="darkgray" fill="blue"/>
    
                            <path id="green" d="
                            M 95 52.5 
                            Q 90 90, 52.5 95 
                            v -22.5
                            Q 70 70, 72.5 52.5
                            Z
                            " 
                            stroke="darkgray" fill="green"/>
    
                            <path id="red" d="
                            M 47.5 95 
                            Q 10 90, 5 52.5 
                            h 22.5
                            Q 30 70, 47.5 72.5
                            Z
                            " 
                            stroke="darkgray" fill="red"/>
                        </svg>
    
    
                    </div>
                </div>
            </div>
            <script type="text/javascript" src="sizing.js"></script>
            <script type="text/javascript" src="gamePlay.js"></script>
    
        </body>
    </html>
    

    JavaScript:

    const start = document.getElementById("start");
    const reset = document.getElementById("reset");
    
    const yellow = document.getElementById("yellow");
    const blue = document.getElementById("blue");
    const green = document.getElementById("green");
    const red = document.getElementById("red");
    
    var order = [];
    var selected = [];
    
    var running;
    
    
    function play(){
        if (running == true){
            yellow.addEventListener('click', () => {
                console.log("yellow has been clicked");
                selected.push("yellow");
            });
    
            blue.addEventListener('click', () => {
                console.log("blue has been clicked");
                selected.push("blue");
            });
    
            green.addEventListener('click', () => {
                console.log("green has been clicked");
                selected.push("green");
            });
    
            red.addEventListener('click', () => {
                console.log("red has been clicked");
                selected.push("red");
            });
        }
    }
    
    
    start.addEventListener('click', () => {
        running = true;
    });
    
    reset.addEventListener('click', () => {
        running = false;
        order = [];
        selected = [];
    });
    
    play();
    

    尺寸。js公司

    const game = document.getElementById("game");
    const blackCircle = document.getElementById("blackCircle");
    const centerCircle = document.getElementById("centerCircle");
    
    function resizing(){
    
        var gameSTYLE = getComputedStyle(game);
        var blackCircleSTYLE = getComputedStyle(blackCircle);
        var centerCircleSTYLE = getComputedStyle(centerCircle);
    
        if (gameSTYLE.width > gameSTYLE.height){
            blackCircle.style.height = parseInt(gameSTYLE.height) - (parseInt(gameSTYLE.height) * .06) + "px";
            blackCircle.style.width = blackCircleSTYLE.height;
            blackCircle.style.top = "3%";
            blackCircle.style.left = (parseInt(gameSTYLE.width)/2) - (parseInt(blackCircleSTYLE.width)/2) + "px";
        } else {
            blackCircle.style.width = parseInt(gameSTYLE.width) - (parseInt(gameSTYLE.width) * .06) + "px";
            blackCircle.style.height = blackCircle.style.width;
            blackCircle.style.left = "3%";
            blackCircle.style.top = (parseInt(gameSTYLE.height)/2) - (parseInt(blackCircleSTYLE.height)/2) + "px";
    
        }
    
        centerCircle.style.height = (parseInt(blackCircleSTYLE.height)*.35) + "px";
        centerCircle.style.width = (parseInt(blackCircleSTYLE.width)*.35) + "px";
        centerCircle.style.top = (parseInt(blackCircleSTYLE.height)/2) - (parseInt(centerCircleSTYLE.height)/2) + "px";
        centerCircle.style.left = (parseInt(blackCircleSTYLE.width)/2) - (parseInt(centerCircleSTYLE.width)/2) + "px";
    
    }
    
    var refresh = setInterval(function(){
        resizing();
    }, .1);
    

    html {
        background: black;
    }
    
    body {
        width: 100vh;
        height: 100vh;
    }
    
    #board {
        position: absolute;
        width: 75%;
        height: 85%;
        top: 8.5%;
        left: 12.5%;
        background-color: gray;
    }
    
    #scoreboard {
        position: absolute;
        width: 15%;
        height: 17.5%;
        top: 5%;
        left: 3%;
    
        background-color: lightgray;
    
        text-align: center;
        font-size: 150%;
        line-height: 150%;
    
        font-weight: bold;
    }
    
    #score {
        line-height: 0;
        font-weight: normal;
    }
    
    #start {
        position: absolute;
        width: 15%;
        height: 8%;
        top: 27.5%;
        left: 3%;
    
        background-color: lightgray;
    
        text-align: center;
        font-size: 150%;
        line-height: 150%;
    
        font-weight: bold;
    }
    
    #start:hover {
        background-color: lightgreen;
    }
    
    #start:active{
        background-color: rgb(98, 161, 98);
    }
    
    #reset {
        position: absolute;
        width: 15%;
        height: 8%;
        top: 37.5%;
        left: 3%;
    
        background-color: lightgray;
    
        text-align: center;
        font-size: 150%;
        line-height: 150%;
    
        font-weight: bold;
    }
    
    #reset:hover {
        background-color: lightcoral;
    }
    
    #reset:active{
        background-color: rgb(202, 99, 99);
    }
    
    #rules{
        position: absolute;
        width: 15%;
        height: 45%;
        top: 50%;
        left: 3%;
    
        background-color: lightgray;
    
        text-align: center;
        font-size: 100%;
        line-height: 100%;
    
    }
    
    #rulesTitle{
        font-size: 125%;
        font-weight: bold;
        text-decoration: underline;
    }
    
    #goodluck{
        font-size: 112.5%;
        font-weight: bold;
    }
    
    #game{
        position: absolute;
        width: 76%;
        height: 90%;
        top: 5%;
        left: 21%;
    
        background-color: lightgray;
    }
    
    #blackCircle {
        position: absolute;
        border-radius: 50%;
    
        background-color: black;
    }
    
    #centerCircle {
        position: absolute;
        border-radius: 50%;
    
        background-color: gray;
    }
    
    
    
    #yellow:hover {
        fill: darkgoldenrod;
    }
    
    #yellow:active {
        fill: black;
    }
    
    
    #blue:hover {
        fill: darkblue;
    }
    
    #blue:active {
        fill: black;
    }
    
    
    #green:hover {
        fill: darkgreen;
    }
    
    #green:active {
        fill: black;
    }
    
    
    #red:hover {
        fill: darkred;
    }
    
    #red:active {
        fill: black;
    }
    
    
    1 回复  |  直到 3 年前
        1
  •  1
  •   Chang Alex    3 年前

    您的代码,如果运行,则侦听click事件, 但是

    之前:

    //gamePlay.js
    
    const start = document.getElementById("start");
    const reset = document.getElementById("reset");
    
    const yellow = document.getElementById("yellow");
    const blue = document.getElementById("blue");
    const green = document.getElementById("green");
    const red = document.getElementById("red");
    
    var order = [];
    var selected = [];
    
    var running;
    
    
    function play(){
        if (running == true){
            yellow.addEventListener('click', () => {
                console.log("yellow has been clicked");
                selected.push("yellow");
            });
    
            blue.addEventListener('click', () => {
                console.log("blue has been clicked");
                selected.push("blue");
            });
    
            green.addEventListener('click', () => {
                console.log("green has been clicked");
                selected.push("green");
            });
    
            red.addEventListener('click', () => {
                console.log("red has been clicked");
                selected.push("red");
            });
        }
    }
    
    
    start.addEventListener('click', () => {
        running = true;
    });
    
    reset.addEventListener('click', () => {
        running = false;
        order = [];
        selected = [];
    });
    
    play();
    
    
    ///////////////////////////////////////////////////////////////////////////
    
    //sizing.js
    
    const game = document.getElementById("game");
    const blackCircle = document.getElementById("blackCircle");
    const centerCircle = document.getElementById("centerCircle");
    
    function resizing(){
    
        var gameSTYLE = getComputedStyle(game);
        var blackCircleSTYLE = getComputedStyle(blackCircle);
        var centerCircleSTYLE = getComputedStyle(centerCircle);
    
        if (gameSTYLE.width > gameSTYLE.height){
            blackCircle.style.height = parseInt(gameSTYLE.height) - (parseInt(gameSTYLE.height) * .06) + "px";
            blackCircle.style.width = blackCircleSTYLE.height;
            blackCircle.style.top = "3%";
            blackCircle.style.left = (parseInt(gameSTYLE.width)/2) - (parseInt(blackCircleSTYLE.width)/2) + "px";
        } else {
            blackCircle.style.width = parseInt(gameSTYLE.width) - (parseInt(gameSTYLE.width) * .06) + "px";
            blackCircle.style.height = blackCircle.style.width;
            blackCircle.style.left = "3%";
            blackCircle.style.top = (parseInt(gameSTYLE.height)/2) - (parseInt(blackCircleSTYLE.height)/2) + "px";
    
        }
    
        centerCircle.style.height = (parseInt(blackCircleSTYLE.height)*.35) + "px";
        centerCircle.style.width = (parseInt(blackCircleSTYLE.width)*.35) + "px";
        centerCircle.style.top = (parseInt(blackCircleSTYLE.height)/2) - (parseInt(centerCircleSTYLE.height)/2) + "px";
        centerCircle.style.left = (parseInt(blackCircleSTYLE.width)/2) - (parseInt(centerCircleSTYLE.width)/2) + "px";
    
    }
    
    var refresh = setInterval(function(){
        resizing();
    }, .1);
    html {
        background: black;
    }
    
    body {
        width: 100vh;
        height: 100vh;
    }
    
    #board {
        position: absolute;
        width: 75%;
        height: 85%;
        top: 8.5%;
        left: 12.5%;
        background-color: gray;
    }
    
    #scoreboard {
        position: absolute;
        width: 15%;
        height: 17.5%;
        top: 5%;
        left: 3%;
    
        background-color: lightgray;
    
        text-align: center;
        font-size: 150%;
        line-height: 150%;
    
        font-weight: bold;
    }
    
    #score {
        line-height: 0;
        font-weight: normal;
    }
    
    #start {
        position: absolute;
        width: 15%;
        height: 8%;
        top: 27.5%;
        left: 3%;
    
        background-color: lightgray;
    
        text-align: center;
        font-size: 150%;
        line-height: 150%;
    
        font-weight: bold;
    }
    
    #start:hover {
        background-color: lightgreen;
    }
    
    #start:active{
        background-color: rgb(98, 161, 98);
    }
    
    #reset {
        position: absolute;
        width: 15%;
        height: 8%;
        top: 37.5%;
        left: 3%;
    
        background-color: lightgray;
    
        text-align: center;
        font-size: 150%;
        line-height: 150%;
    
        font-weight: bold;
    }
    
    #reset:hover {
        background-color: lightcoral;
    }
    
    #reset:active{
        background-color: rgb(202, 99, 99);
    }
    
    #rules{
        position: absolute;
        width: 15%;
        height: 45%;
        top: 50%;
        left: 3%;
    
        background-color: lightgray;
    
        text-align: center;
        font-size: 100%;
        line-height: 100%;
    
    }
    
    #rulesTitle{
        font-size: 125%;
        font-weight: bold;
        text-decoration: underline;
    }
    
    #goodluck{
        font-size: 112.5%;
        font-weight: bold;
    }
    
    #game{
        position: absolute;
        width: 76%;
        height: 90%;
        top: 5%;
        left: 21%;
    
        background-color: lightgray;
    }
    
    #blackCircle {
        position: absolute;
        border-radius: 50%;
    
        background-color: black;
    }
    
    #centerCircle {
        position: absolute;
        border-radius: 50%;
    
        background-color: gray;
    }
    
    
    
    #yellow:hover {
        fill: darkgoldenrod;
    }
    
    #yellow:active {
        fill: black;
    }
    
    
    #blue:hover {
        fill: darkblue;
    }
    
    #blue:active {
        fill: black;
    }
    
    
    #green:hover {
        fill: darkgreen;
    }
    
    #green:active {
        fill: black;
    }
    
    
    #red:hover {
        fill: darkred;
    }
    
    #red:active {
        fill: black;
    }
    <!DOCTYPE html>
    <html>
        <head>
            <title>Simon Game</title>
            <link rel="stylesheet" href="style.css" />
        </head>
        <body>
            <div id="board">
                <div id="scoreboard">
                    Score:
                    <br>
                    <p id="score">0</p>
                </div>
    
                <button id="start">START</button>
                <button id="reset">RESET</button>
    
                <div id="rules">
                    <p id="rulesTitle">Rules</p>
                    The rules are simple, memorize the pattern shown and repeat it. Every round, the pattern gets longer. If you mess up even once, the game ends.
                    <p id="goodluck">Good luck!</p>
                </div>
    
                <div id="game">
                    <div id="blackCircle">
                        <div id="centerCircle"></div>
    
    
                        <svg width="100%" height="100%" viewBox="0 0 100 100" preserveAspectRatio="none">
                            <path id="yellow" d="
                            M 5 47.5 
                            Q 10 10, 47.5 5 
                            v 22.5
                            Q 30 30, 27.5 47.5
                            Z
                            " 
                            stroke="darkgray" fill="yellow"/>
    
                            <path id="blue" d="
                            M 52.5 5 
                            Q 90 10, 95 47.5 
                            h -22.5
                            Q 70 30, 52.5 27.5
                            Z
                            " 
                            stroke="darkgray" fill="blue"/>
    
                            <path id="green" d="
                            M 95 52.5 
                            Q 90 90, 52.5 95 
                            v -22.5
                            Q 70 70, 72.5 52.5
                            Z
                            " 
                            stroke="darkgray" fill="green"/>
    
                            <path id="red" d="
                            M 47.5 95 
                            Q 10 90, 5 52.5 
                            h 22.5
                            Q 30 70, 47.5 72.5
                            Z
                            " 
                            stroke="darkgray" fill="red"/>
                        </svg>
    
    
                    </div>
                </div>
            </div>
            <script type="text/javascript" src="sizing.js"></script>
            <script type="text/javascript" src="gamePlay.js"></script>
    
        </body>
    </html>

    之后:

    //gamePlay.js
    
    const start = document.getElementById("start");
    const reset = document.getElementById("reset");
    
    const yellow = document.getElementById("yellow");
    const blue = document.getElementById("blue");
    const green = document.getElementById("green");
    const red = document.getElementById("red");
    
    var order = [];
    var selected = [];
    
    var running;
    
    
    function play() {
      yellow.addEventListener('click', () => {
        if (!running) return;
        console.log("yellow has been clicked");
        selected.push("yellow");
      });
    
      blue.addEventListener('click', () => {
        if (!running) return;
        console.log("blue has been clicked");
        selected.push("blue");
      });
    
      green.addEventListener('click', () => {
        if (!running) return;
        console.log("green has been clicked");
        selected.push("green");
      });
    
      red.addEventListener('click', () => {
        if (!running) return;
        console.log("red has been clicked");
        selected.push("red");
      });
    }
    
    
    start.addEventListener('click', () => {
      running = true;
    });
    
    reset.addEventListener('click', () => {
      running = false;
      order = [];
      selected = [];
    });
    
    play();
    
    
    ///////////////////////////////////////////////////////////////////////////
    
    //sizing.js
    
    const game = document.getElementById("game");
    const blackCircle = document.getElementById("blackCircle");
    const centerCircle = document.getElementById("centerCircle");
    
    function resizing() {
    
      var gameSTYLE = getComputedStyle(game);
      var blackCircleSTYLE = getComputedStyle(blackCircle);
      var centerCircleSTYLE = getComputedStyle(centerCircle);
    
      if (gameSTYLE.width > gameSTYLE.height) {
        blackCircle.style.height = parseInt(gameSTYLE.height) - (parseInt(gameSTYLE.height) * .06) + "px";
        blackCircle.style.width = blackCircleSTYLE.height;
        blackCircle.style.top = "3%";
        blackCircle.style.left = (parseInt(gameSTYLE.width) / 2) - (parseInt(blackCircleSTYLE.width) / 2) + "px";
      } else {
        blackCircle.style.width = parseInt(gameSTYLE.width) - (parseInt(gameSTYLE.width) * .06) + "px";
        blackCircle.style.height = blackCircle.style.width;
        blackCircle.style.left = "3%";
        blackCircle.style.top = (parseInt(gameSTYLE.height) / 2) - (parseInt(blackCircleSTYLE.height) / 2) + "px";
    
      }
    
      centerCircle.style.height = (parseInt(blackCircleSTYLE.height) * .35) + "px";
      centerCircle.style.width = (parseInt(blackCircleSTYLE.width) * .35) + "px";
      centerCircle.style.top = (parseInt(blackCircleSTYLE.height) / 2) - (parseInt(centerCircleSTYLE.height) / 2) + "px";
      centerCircle.style.left = (parseInt(blackCircleSTYLE.width) / 2) - (parseInt(centerCircleSTYLE.width) / 2) + "px";
    
    }
    
    var refresh = setInterval(function() {
      resizing();
    }, .1);
    }
    
    车身{
    宽度:100vh;
    高度:100vh;
    }
    
    宽度:75%;
    左:12.5%;
    背景色:灰色;
    }
    
    #记分牌{
    位置:绝对;
    身高:17.5%;
    左:3%;
    
    背景色:浅灰色;
    
    字号:150%;
    线高:150%;
    
    }
    
    #分数{
    字体重量:正常;
    }
    
    #开始{
    位置:绝对;
    身高:8%;
    顶部:27.5%;
    
    背景色:浅灰色;
    
    字号:150%;
    线高:150%;
    
    }
    
    背景色:浅绿色;
    }
    
    #开始:活动{
    背景色:rgb(98、161、98);
    
    #重置{
    位置:绝对;
    身高:8%;
    顶部:37.5%;
    左:3%;
    
    背景色:浅灰色;
    
    文本对齐:居中;
    字号:150%;
    线高:150%;
    
    字号:粗体;
    }
    
    背景色:浅珊瑚色;
    }
    
    #重置:激活{
    背景色:rgb(202、99、99);
    
    #规则{
    位置:绝对;
    宽度:15%;
    顶部:50%;
    左:3%;
    
    背景色:浅灰色;
    
    字体大小:100%;
    
    }
    
    #规则针{
    字号:粗体;
    文字装饰:下划线;
    }
    
    #祝你好运{
    字号:112.5%;
    字号:粗体;
    
    #游戏{
    位置:绝对;
    身高:90%;
    顶部:5%;
    左:21%;
    
    背景色:浅灰色;
    
    #黑圈{
    
    背景色:黑色;
    
    #中心圆{
    
    背景色:灰色;
    }
    
    
    
    #黄色:悬停{
    填充物:黑加仑;
    }
    
    #黄色:活动{
    }
    
    
    #蓝色:悬停{
    }
    
    填充:黑色;
    }
    
    
    填充:暗绿色;
    }
    
    #绿色:活动{
    填充:黑色;
    
    
    #红色:悬停{
    填充:深色;
    }
    
    #红色:激活{
    }
    <!DOCTYPE html>
    <水头>;
    <link rel=“stylesheet”href=“style.css”/>
    <车身>
    <div id=“board”>
    分数:
    <br>
    </div>
    
    <按钮id=“开始”>启动(<)/按钮(>);
    
    <div id=“规则”>
    规则很简单,记住所示的模式并重复。每一轮,图案都会变长。如果你搞砸了一次,游戏就结束了。
    </div>
    
    <div id=“游戏”>
    <div id=“黑圈”>
    <div id=“中心圆”></div>
    
    
    <svg width=“100%”height=“100%”viewBox=“0 0 100 100preserveSpectratio=“none”>
    <路径id=“黄色”d=”
    M 5 47.5
    问题10,47.5 5
    v 22.5
    Q 3027.547.5
    Z
    stroke=“darkgray”fill=“yellow”/>
    
    Q 90 10,95 47.5
    h-22.5
    Q 70 30,52.5 27.5
    Z
    "
    
    <路径id=“绿色”d=”
    M 95 52.5
    v-22.5
    Z
    "
    笔划=“darkgray”填充=“绿色”/>
    
    <路径id=“红色”d=”
    M 47.5 95
    Q 10 90,5 52.5
    h 22.5
    Z
    "
    </svg>
    
    
    </div>
    </div>
    </div>
    <script type=“text/javascriptsrc=“size.js”></脚本(>);
    <script type=“text/javascriptsrc=“gamePlay.js”></脚本(>);
    
    </车身>
    </html>

    还有一件事: