点击图像不按预期工作

huangapple go评论84阅读模式
英文:

Clicking on an image not working as intended

问题

我创建了一个游戏,你需要点击屏幕左侧的图像,但必须选择不在对面出现的图像才能进入下一关。但出现一个问题,当你点击左侧的任何图像时,仍然会进入下一关。当你点击错误的图像时,应该显示游戏结束。目前只有在点击空白处时才会出现游戏结束的提示。有什么建议吗?

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8" />
    <title>Matching Game</title>
    <style>
        img {
            position: absolute;
        }
        div {
            position: absolute;
            width: 500px;
            height: 500px;
        }
        #rightSide {
            left: 500px;
            border-left: 1px solid;
        }
    </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>
        let numberOfFaces = 5;
        const theLeftSide = document.querySelector("#leftSide");
        const theRightSide = document.querySelector("#rightSide");

        function generateFaces() {
            for (let i = 0; i < numberOfFaces; i++) {
                let face = document.createElement("img");
                face.src = 'images/smile.png';

                const randomTop = Math.floor(Math.random() * 400) + 1;
                const randomLeft = Math.floor(Math.random() * 400) + 1;

                face.style.top = randomTop + 'px';
                face.style.left = randomLeft + 'px';

                theLeftSide.appendChild(face);

                theLeftSide.lastChild.addEventListener('click', nextLevel);
                document.body.addEventListener('click', gameOver);
            }

            const leftSideImages = theLeftSide.cloneNode(true);
            leftSideImages.removeChild(leftSideImages.lastChild);
            theRightSide.appendChild(leftSideImages);

            function nextLevel(event) {
                event.stopPropagation();
                numberOfFaces += 5;

                while (theLeftSide.firstChild) {
                    theLeftSide.removeChild(theLeftSide.firstChild);
                }

                while (theRightSide.firstChild) {
                    theRightSide.removeChild(theRightSide.firstChild);
                }

                generateFaces();
            }

            function gameOver() {
                alert('Game Over!');
                document.body.removeEventListener('click', gameOver);
                theLeftSide.lastChild.removeEventListener('click', nextLevel);
            }

        }
    </script>
</body>
</html>

点击图像不按预期工作

英文:

I made a game where you need to click the image on the left side of the screen that you do not see on the opposite side to get to the next level. For some reason, when you click any image on the left side you still go to the next level. When you click the wrong image it should say game over. Right now it only does that when the whitespace is clicked. Any tips?

点击图像不按预期工作

&lt;!DOCTYPE html&gt;
&lt;html lang=&quot;en&quot;&gt;
&lt;head&gt;
&lt;meta charset=&quot;utf-8&quot; /&gt;
&lt;title&gt;Matching Game&lt;/title&gt;
&lt;style&gt;
img {
position: absolute;
}  
div {
position: absolute;
width: 500px;
height: 500px;
}
#rightSide {
left: 500px;
border-left: 1px solid;
}
&lt;/style&gt;
&lt;/head&gt;
&lt;body onload=&quot;generateFaces()&quot;&gt;
&lt;h1&gt;Matching Game&lt;/h1&gt;
&lt;p&gt;Click on the extra smiling face on the left.&lt;/p&gt;
&lt;div id=&quot;leftSide&quot;&gt;&lt;/div&gt;
&lt;div id=&quot;rightSide&quot;&gt;&lt;/div&gt;
&lt;script &gt;
let numberOfFaces = 5;
const theLeftSide = document.querySelector(&quot;#leftSide&quot;);
const theRightSide = document.querySelector(&quot;#rightSide&quot;);
function generateFaces() {
for (let i = 0; i &lt; numberOfFaces; i++) {
let face = document.createElement(&quot;img&quot;);
face.src = &#39;images/smile.png&#39;;
const randomTop = Math.floor(Math.random() * 400) + 1;
const randomLeft = Math.floor(Math.random() * 400) + 1;
face.style.top = randomTop + &#39;px&#39;;
face.style.left = randomLeft + &#39;px&#39;;
theLeftSide.appendChild(face);
theLeftSide.lastChild.addEventListener(&#39;click&#39;, nextLevel);
document.body.addEventListener(&#39;click&#39;, gameOver);
}   
const leftSideImages = theLeftSide.cloneNode(true);
leftSideImages.removeChild(leftSideImages.lastChild);
theRightSide.appendChild(leftSideImages); 
function nextLevel(event) {
event.stopPropagation();
numberOfFaces += 5;
while (theLeftSide.firstChild) {
theLeftSide.removeChild(theLeftSide.firstChild);
}
while (theRightSide.firstChild) {
theRightSide.removeChild(theRightSide.firstChild);
}
generateFaces();
}
function gameOver() {
alert(&#39;Game Over!&#39;);
document.body.removeEventListener(&#39;click&#39;, gameOver);
theLeftSide.lastChild.removeEventListener(&#39;click&#39;, nextLevel);
}
} 
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;

答案1

得分: 2

theLeftSide.lastChild.addEventListener(&#39;click&#39;, nextLevel);放在循环外部以使其正常工作。

英文:

notice

for (let i = 0; i &lt; numberOfFaces; i++) {
    let face = document.createElement(&quot;img&quot;);
    ...
    theLeftSide.appendChild(face);

    // you do this in every loop
    theLeftSide.lastChild.addEventListener(&#39;click&#39;, nextLevel);
}   

put the theLeftSide.lastChild.addEventListener(&#39;click&#39;, nextLevel); outside loop for it to work

huangapple
  • 本文由 发表于 2023年1月9日 08:15:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75052213.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定