gpt4 book ai didi

javascript - Canvas 游戏不更新分数?

转载 作者:行者123 更新时间:2023-11-28 20:28:44 26 4
gpt4 key购买 nike

你好,我做了一个贪吃蛇游戏,一切都很好,除了我想更新一个文本字段,如果它们死后的分数高于之前的分数,最终我会列出前 10 名列表等。

我的代码是:

score2 = document.getElementsByName('updateScore').value;
if(score > score2)
{
document.getElementById('updateScore').innerHTML = score;
}

在网络游戏死亡时重置分数之前,会在 init 方法中调用此函数

您可以在此处查看游戏和源代码: http://www.taffatech.com/Snake.html

最佳答案

您应该使用getElementById()而不是getElementsByName()。您的文本框有 id updateScore 但没有名称:

<input type="textbox" id="updateScore" width ="20"/>

您应该使用此代码:

score2 = document.getElementById('updateScore').value;
if(score > score2) {
document.getElementById('updateScore').value = score;
}

我已经在您的游戏中对其进行了测试,并且它有效。

关于javascript - Canvas 游戏不更新分数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16844136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com