当马里奥跳过管道时,我试图让分数增加+1个单位,但当这种情况发生时,分数增加了超过1个单位.我怎么能这样做呢?

const mario = document.querySelector('.mario');
const pipe = document.querySelector('.pipe');

const jump = () => {
  mario.classList.add('jump');

  setTimeout(() => {
    mario.classList.remove('jump');
  }, 500);
}

let score = 0;

function scoreCount() {
  score++;

  document.querySelector('#score').innerHTML = `SCORE: ${score}`;
}

const loop = setInterval(() => {

  const pipePosition = pipe.offsetLeft;
  const marioPosition = +window.getComputedStyle(mario).bottom.replace('px', '');

  if (pipePosition < 0 && marioPosition >= 80) {
    scoreCount();
  }

  if (pipePosition <= 120 && pipePosition > 0 && marioPosition < 80) {

    pipe.style.animation = 'none';
    pipe.style.left = `${pipePosition}px`;

    mario.style.animation = 'none';
    mario.style.bottom = `${marioPosition}px`;

    mario.src = './marioImages/game-over.png';
    mario.style.width = '75px';
    mario.style.margin = '50px';

    clearInterval(loop);
  }
}, 10)

document.addEventListener('keydown', jump);
* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

#score {
  font-family: 'Super Mario 256';
  margin: 10px;
  z-index: 3;
}

.game-board {
  width: 100%;
  height: 500px;
  border-bottom: 15px solid rgb(35, 160, 35);
  margin: 0 auto;
  position: relative;
  overflow: hidden;
  background: linear-gradient(#87CEEB, #E0F6FF);
}

.pipe {
  position: absolute;
  bottom: 0;
  width: 80px;
  animation: pipe-animation 1.5s infinite linear;
}

.mario {
  position: absolute;
  width: 150px;
  bottom: 0;
  z-index: 2;
}

.jump {
  animation: jump 500ms ease-out;
}

.clouds {
  position: absolute;
  width: 500px;
  animation: clouds-animation 4s infinite linear;
  z-index: 1;
}

@keyframes pipe-animation {
  from {
    right: -80px;
  }
  to {
    right: 100%;
  }
}

@keyframes jump {
  0% {
    bottom: 0;
  }
  40% {
    bottom: 180px;
  }
  50% {
    bottom: 180px;
  }
  60% {
    bottom: 180px;
  }
  100% {
    bottom: 0;
  }
}

@keyframes clouds-animation {
  from {
    right: -500px;
  }
  to {
    right: 100%;
  }
}

@font-face {
  font-family: 'Super Mario 256';
  src: url(/marioFont/SuperMario256.ttf);
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <link rel="stylesheet" href="marioCSS/mario.css">
  <script src="marioJS/mario.js" defer></script>
  <title>Mario Jump</title>
</head>

<body>
  <div class="game-board">
    <h2 id="score">SCORE: 0</h2>
    <img src="marioImages/clouds.png" class="clouds">
    <img src="marioImages/mario.gif" class="mario">
    <img src="marioImages/pipe.png" class="pipe">
  </div>
</body>

</html>

我试图创建一个函数scoreCount来增加分数.

然后,我将scoreCount函数调用到"循环"中,当"pipePosition"移出窗口时,将分数加+1.

但它的增量远远超过1.

推荐答案

因此,似乎在setInterval循环中调用了scoreCount(),该循环将不断调用该函数并重复递增分数.

如果你仍然想把它放在循环中,我推荐以下几点:

  • 创建一个全局变量"Score Set",并将其设置为false
  • 添加一条if语句以判断"Score Set"是否为真
  • 如果为真,则阻止分数再次递增
  • 如果为假,则让它增加分数,但将scoreSet设置为真

使用此示例的最终代码:

const mario = document.querySelector('.mario');
const pipe = document.querySelector('.pipe');

const jump = () => {
  mario.classList.add('jump');
  setTimeout(() => {
    mario.classList.remove('jump');
  }, 500);
}

let score = 0;
let scoreSet = false; // Variable to check whether you already incremented the score

function scoreCount() {
  if (!scoreSet) {
    score++;
    scoreSet = true;
  }

  document.querySelector('#score').innerHTML = `SCORE: ${score}`;
}
const loop = setInterval(() => {
  const pipePosition = pipe.offsetLeft;
  const marioPosition = +window.getComputedStyle(mario).bottom.replace('px', '');
  if (pipePosition < 0 && marioPosition >= 80) {
    scoreCount();
  }
  if (pipePosition <= 120 && pipePosition > 0 && marioPosition < 80) {
    pipe.style.animation = 'none';
    pipe.style.left = `${pipePosition}px`;
    mario.style.animation = 'none';
    mario.style.bottom = `${marioPosition}px`;
    mario.src = './marioImages/game-over.png';
    mario.style.width = '75px';
    mario.style.margin = '50px';
    clearInterval(loop);
  }
}, 10);

document.addEventListener('keydown', jump);

如果您想在脚本中的某个时候再次增加分数,只需使用"ScotreSet=False"即可.

Javascript相关问答推荐

使用JavaScript重命名对象数组中的键

JavaScript文本区域阻止KeyDown/KeyUp事件本身上的Alt GR +键组合

materialized - activeIndex返回-1

IMDB使用 puppeteer 加载更多按钮(nodejs)

Angular中计算信号和getter的区别

为什么我的列表直到下一次提交才更新值/onChange

如何在不创建新键的情况下动态更改 map 中的项目?

给定一个凸多边形作为一组边,如何根据到最近边的距离填充里面的区域

cypress中e2e测试上的Click()事件在Switch Element Plus组件上使用时不起作用

使用原型判断对象是否为类的实例

未定义引用错误:未定义&Quot;而不是&Quot;ReferenceError:在初始化&Quot;之前无法访问';a';

我可以使用空手道用户界面来获取网页的当前滚动位置吗?

查询参数未在我的Next.js应用路由接口中定义

在使用位板时,如何在Java脚本中判断Connect 4板中中柱的对称性?

SPAN不会在点击时关闭模式,尽管它们可以发送日志(log)等

按特定顺序将4个数组组合在一起,按ID分组

我在哪里添加过滤器值到这个函数?

限制数组中每个元素的长度,

将字体样式应用于material -UI条形图图例

JSON Web令牌(JWT)错误:RSA密钥对的签名无效