【问题标题】:JavaScript point counter for a html snake like gamehtml 蛇类游戏的 JavaScript 点计数器
【发布时间】:2021-12-19 09:44:18
【问题描述】:

我是学习 JavaScript 的新手,我搜索了很多网站,目前还没有找到解决方案。

我正在制作一个基于蛇的迷你游戏,谁能帮我用javascript和html制作一个点数计数器,

您可以在这里查看游戏!

https://github.com/LiquidSlime/Worm-game

我想用它来完成它

<div id="points"<h1></h1></div>
and javascript;
var element = document.getElementById("points")
**Code here**;

希望有人能帮帮我!

【问题讨论】:

  • element.querySelector('h1').innerText = points;? (很明显,每当points 发生变化时,都需要再次调用,并且只有在不再出现拼写错误时才会起作用)
  • 错字是一个原因?元素 ID - pints,您正在搜索 points
  • 不,但谢谢,我实际上不知道如何整合我的蠕虫游戏和 h1 积分计数器

标签: javascript html 2d-games


【解决方案1】:

您可以使用 id 点创建一个跨度

<span id=points>? Points</span>

然后在js中更改innnerText

const pointElement = document.getElementById('points')

function updatePoints(points) {
  pointElement.innerText = points + ' Points'
}

然后运行更新点

updatePoints(1)

// If you want to load if from another varible

// get points from somewhere else

updatePoints(points)

当您希望它更新时,您的代码中会像这样

points++;
updatePoints(points)

【讨论】:

  • updatePoints(1) if var i = index + 1;我
  • @MaximShevchenko 你不需要updatePoints(1) updatePoints 函数只是写入你的积分元素的数字,所以这会将你的积分更改为1,然后在你的积分再次更新之后(最后),所以不需要第一个updatePoints(1)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-11-12
  • 1970-01-01
  • 2014-08-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多