如何使用 DOM 和 JavaScript 在游戏的 3 个不同级别之间切换?例如简单、中等和困难

发布于 2024-11-07 10:46:54 字数 697 浏览 0 评论 0原文

我正在尝试使用 DOM 和 JavaScript 制作一个简单的游戏,在这个游戏中我有 3 个不同的级别:简单、中等和困难。默认情况下,当页面加载时,使用简单级别!但我希望能够使用 javascript 在 3 个不同级别之间切换!

可以使用 javascript cookie 会话来完成此操作,因此当单击按钮时将使用/激活特定功能,然后使用该功能直到用户单击另一个按钮(例如中等级别)。

例如:

function easylevel() {
}

function mediumlevel() {
}

function hardlevel() {
}

因此,通过单击按钮,上述任何功能都会被激活并存储到 cookie 会话中,例如:

<input type="button" onclick="easylevel()" value="Easy Level" />
<input type="button" onclick="mediumlevel()" value="Medium Level" />
<input type="button" onclick="hardlevel()" value="Hard Level" />

我已经尝试过这个,但它不起作用,有人可以解释一下我哪里出了问题吗? !我200%确定我错了,因为我对JS了解不多,所以肯定需要帮助和建议!

I'm trying making a simple game using DOM and JavaScript, and within this game I've 3 different levels: easy, medium and hard. By default when the page loads the easy level is used! But I want to be able to switch between the 3 different levels using javascript!

It is possible to do this using javascript cookie session, so a particular function is being used/activated when a button is clicked, then that function is used until the user clicks on another button, say for example the medium level.

For example:

function easylevel() {
}

function mediumlevel() {
}

function hardlevel() {
}

So any of the above functions is activated and stored into a cookie session by clicking a button, for example:

<input type="button" onclick="easylevel()" value="Easy Level" />
<input type="button" onclick="mediumlevel()" value="Medium Level" />
<input type="button" onclick="hardlevel()" value="Hard Level" />

I've already tried this, but it doesn't works, can somebody please explain me where I'm going wrong! I'm 200% sure that I'm wrong because I don't know much about JS, so need help and advice for sure!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

江城子 2024-11-14 10:46:54

如果你想要的是重复调用一个函数,那么你可以使用 setInterval

<script>
var intervalInMilliseconds = 1000; // change this to watever value you like
var activeInterval = undefined;
function startEasyLevel() {
    if (activeInterval) {
         clearInterval(activeInterval);
    }
    activeInterval = setInterval(easylevel, intervalInMilliseconds);
}
function startMediumLevel() {
    if (activeInterval) {
         clearInterval(activeInterval);
    }
    activeInterval = setInterval(mediumlevel, intervalInMilliseconds);
}
function startHardLevel() {
    if (activeInterval) {
         clearInterval(activeInterval);
    }
    activeInterval = setInterval(hardlevel, intervalInMilliseconds);
}
</script>

<input type="button" onclick="startEasyLevel()" value="Easy Level" />
<input type="button" onclick="startMediumLevel()" value="Medium Level" />
<input type="button" onclick="startHardLevel()" value="Hard Level" />

添加你当前级别的函数,它应该每秒调用该函数。一旦您单击其中一个按钮,它将停止调用当前级别的函数,并继续调用与单击的按钮关联的函数。

如果你想默认加载某个关卡,你可以使用 window 元素的 onload 事件:

window.onload = startEasyLevel;

If what you want is that a function is called repeatedly, then you could use setInterval

<script>
var intervalInMilliseconds = 1000; // change this to watever value you like
var activeInterval = undefined;
function startEasyLevel() {
    if (activeInterval) {
         clearInterval(activeInterval);
    }
    activeInterval = setInterval(easylevel, intervalInMilliseconds);
}
function startMediumLevel() {
    if (activeInterval) {
         clearInterval(activeInterval);
    }
    activeInterval = setInterval(mediumlevel, intervalInMilliseconds);
}
function startHardLevel() {
    if (activeInterval) {
         clearInterval(activeInterval);
    }
    activeInterval = setInterval(hardlevel, intervalInMilliseconds);
}
</script>

<input type="button" onclick="startEasyLevel()" value="Easy Level" />
<input type="button" onclick="startMediumLevel()" value="Medium Level" />
<input type="button" onclick="startHardLevel()" value="Hard Level" />

Add to this your current level functions and it should call the function every second. As soon as you click one of your buttons it will stop calling the current level function and continue to call the function associated with the clicked button.

If you want some level be loaded by default you can use the onload event of the window element:

window.onload = startEasyLevel;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文