JavaScript跳转功能失败,找不到错误
JavaScript jump function failing, cannot find error
我在 JavaScript、CSS 和 HTML 中开始了一个简单的游戏,并且在我创建跳跃功能之前游戏一直在运行。单击时,字符元素(由 css 生成的绿色矩形)应该跳跃。相反,它是静止的,不会移动。
最初,我有这个代码:
.animate{
animation: jump 500ms;
}
在角色的CSS中,它在没有任何用户交互的情况下连续跳跃。
我做错了什么?我认为这与此有关:
function jump(){
character.classList.add("animate");
setTimeout(function(){
character.classList.remove("animate");
},500);
}
如能指出错误,并提供新代码和关于 setTimeout 函数工作原理的清晰解释,我将不胜感激。 setTimeout 是现有的内置方法吗?如果是,我们在哪里可以找到有关这些内容的文档?
代码
var character = document.getElementById("character");
var enemy = document.getElementById("enemy");
//adding the animate function in the css here, so it is applied to our character
function jump() {
character.classList.add("animate");
setTimeout(function() {
character.classList.remove("animate");
}, 500);
}
* {
padding: 0;
margin: 22;
}
#game {
width: 500px;
height: 500px;
border: 1px solid #319b4e;
}
#character {
width: 30px;
height: 120px;
background-color: green;
position: relative;
top: 380px;
border-radius: 20px;
/*animation: jump 500ms */
}
/* new class called animate */
.animate {
animation: jump 500ms;
}
#enemy {
width: 60px;
height: 60px;
background-color: red;
border-radius: 14px;
position: relative;
top: 320px;
left: 440px;
animation: moveenemy 1s infinite linear;
}
@keyframes moveenemy {
0% {
left: 440px;
}
50% {
top: 58px;
}
100% {
left: 0px;
top: 320x;
}
}
@keyframes jump {
0% {
top: 380px;
}
30% {
top: 200px;
}
50% {
top: 200px;
}
100% {
top: 380px;
}
}
<!DOCTYPE html>
<html lang="en" onclick="jump()">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Game</h1>
<p>My first ever game</p>
<p>We all have an enemy</p>
<div id="game">
<div id="character"></div>
<div id="enemy"></div>
</div>
<script src="script.js"></script>
</body>
</html>
问题是,在复制代码并测试之后,这个术语
document.getElementById
拼写为小写 b
,如 document.getElementbyId
,这两行应改为:
var character = document.getElementById("character");
var enemy = document.getElementById("enemy");`
至于setTimeout
,它是一个内置函数,在一定时间后(在第二个参数中指定)执行一个函数(在第一个参数中提供),这个函数在后台运行,所以它不会阻止其他事情的发生。
For more information on setTimeout(刚开始学习的时候用过这个网站html等....)
如果您想将其更改为向上箭头,只需添加一个 event listener
来检查 keyup 事件,并检查键码是否与向上箭头 (38) 和其他键的键码匹配可以通过 console.log
ing 事件的 keyCode 来检查,所以在你的 JavaScript 的某个地方,做
addEventListener("keyup", function(e) {
if(e.keyCode == 38) jump()
})
所以...
整个片段是
var character = document.getElementById("character");
var enemy = document.getElementById("enemy");
//adding the animate function in the css here, so it is applied to our character
function jump(){
character.classList.add("animate");
setTimeout(function(){
character.classList.remove("animate");
},500);
}
addEventListener("keyup", function(e) {
if(e.keyCode == 38) jump()
})
*{
padding:0;
margin:22;
}
#game{
width:500px;
height:500px;
border:1px solid #319b4e;
}
#character{
width:30px;
height:120px;
background-color:green;
position:relative;
top:380px;
border-radius:20px;
/*animation: jump 500ms */
}
/* new class called animate */
.animate{
animation: jump 500ms;
}
#enemy{
width:60px;
height:60px;
background-color:red;
border-radius:14px;
position:relative;
top:320px;
left:440px;
animation: moveenemy 1s infinite linear;
}
@keyframes moveenemy{
0%{left:440px;}
50%{top:58px;}
100%{left:0px; top:320x;}
}
@keyframes jump{
0%{top:380px;}
30%{top:200px;}
50%{top:200px;}
100%{top:380px;}
}
<!DOCTYPE html>
<html lang="en" onclick="jump()">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Game</h1>
<p>My first ever game</p>
<p>We all have an enemy</p>
<div id="game">
<div id="character"></div>
<div id="enemy"></div>
</div>
<script src="script.js"></script>
</body>
</html>
正在工作
问题是:你写 document.getElementbyId("character") 但字符 b 的 getElementbyid 很小所以它通过错误
所以正确的是 document.getElementById("character")
var character = document.getElementById("character");
var enemy = document.getElementById("enemy");
//adding the animate function in the css here, so it is applied to our character
function jump(){
character.classList.add("animate");
setTimeout(function(){
character.classList.remove("animate");
},500);
}
*{
padding:0;
margin:22;
}
#game{
width:500px;
height:500px;
border:1px solid #319b4e;
}
#character{
width:30px;
height:120px;
background-color:green;
position:relative;
top:380px;
border-radius:20px;
/*animation: jump 500ms */
}
/* new class called animate */
.animate{
animation: jump 500ms;
}
#enemy{
width:60px;
height:60px;
background-color:red;
border-radius:14px;
position:relative;
top:320px;
left:440px;
animation: moveenemy 1s infinite linear;
}
@keyframes moveenemy{
0%{left:440px;}
50%{top:58px;}
100%{left:0px; top:320x;}
}
@keyframes jump{
0%{top:380px;}
30%{top:200px;}
50%{top:200px;}
100%{top:380px;}
}
<!DOCTYPE html>
<html lang="en" onclick="jump()">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Game</h1>
<p>My first ever game</p>
<p>We all have an enemy</p>
<div id="game">
<div id="character"></div>
<div id="enemy"></div>
</div>
<script src="script.js"></script>
</body>
</html>
Is setTimeout an existing inbuilt method, and if so where do we find documentation on these things?
- 是的,
setTimeout
是一种内置方法。您可以在此处了解更多信息 MDN Web Docs.
how the setTimeout function works
- 因此,
setTimeout
方法接受一个 function/code 和一个延迟(以毫秒为单位),之后它将执行它。参考上面link.
它将事件带入事件循环,使其成为异步调用。
稍后我会查看代码并回复您! :D
收藏MDN Web Docs,真的好用!
我在 JavaScript、CSS 和 HTML 中开始了一个简单的游戏,并且在我创建跳跃功能之前游戏一直在运行。单击时,字符元素(由 css 生成的绿色矩形)应该跳跃。相反,它是静止的,不会移动。
最初,我有这个代码:
.animate{
animation: jump 500ms;
}
在角色的CSS中,它在没有任何用户交互的情况下连续跳跃。
我做错了什么?我认为这与此有关:
function jump(){
character.classList.add("animate");
setTimeout(function(){
character.classList.remove("animate");
},500);
}
如能指出错误,并提供新代码和关于 setTimeout 函数工作原理的清晰解释,我将不胜感激。 setTimeout 是现有的内置方法吗?如果是,我们在哪里可以找到有关这些内容的文档?
代码
var character = document.getElementById("character");
var enemy = document.getElementById("enemy");
//adding the animate function in the css here, so it is applied to our character
function jump() {
character.classList.add("animate");
setTimeout(function() {
character.classList.remove("animate");
}, 500);
}
* {
padding: 0;
margin: 22;
}
#game {
width: 500px;
height: 500px;
border: 1px solid #319b4e;
}
#character {
width: 30px;
height: 120px;
background-color: green;
position: relative;
top: 380px;
border-radius: 20px;
/*animation: jump 500ms */
}
/* new class called animate */
.animate {
animation: jump 500ms;
}
#enemy {
width: 60px;
height: 60px;
background-color: red;
border-radius: 14px;
position: relative;
top: 320px;
left: 440px;
animation: moveenemy 1s infinite linear;
}
@keyframes moveenemy {
0% {
left: 440px;
}
50% {
top: 58px;
}
100% {
left: 0px;
top: 320x;
}
}
@keyframes jump {
0% {
top: 380px;
}
30% {
top: 200px;
}
50% {
top: 200px;
}
100% {
top: 380px;
}
}
<!DOCTYPE html>
<html lang="en" onclick="jump()">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Game</h1>
<p>My first ever game</p>
<p>We all have an enemy</p>
<div id="game">
<div id="character"></div>
<div id="enemy"></div>
</div>
<script src="script.js"></script>
</body>
</html>
问题是,在复制代码并测试之后,这个术语
document.getElementById
拼写为小写 b
,如 document.getElementbyId
,这两行应改为:
var character = document.getElementById("character");
var enemy = document.getElementById("enemy");`
至于setTimeout
,它是一个内置函数,在一定时间后(在第二个参数中指定)执行一个函数(在第一个参数中提供),这个函数在后台运行,所以它不会阻止其他事情的发生。
For more information on setTimeout(刚开始学习的时候用过这个网站html等....)
如果您想将其更改为向上箭头,只需添加一个 event listener
来检查 keyup 事件,并检查键码是否与向上箭头 (38) 和其他键的键码匹配可以通过 console.log
ing 事件的 keyCode 来检查,所以在你的 JavaScript 的某个地方,做
addEventListener("keyup", function(e) {
if(e.keyCode == 38) jump()
})
所以...
整个片段是
var character = document.getElementById("character");
var enemy = document.getElementById("enemy");
//adding the animate function in the css here, so it is applied to our character
function jump(){
character.classList.add("animate");
setTimeout(function(){
character.classList.remove("animate");
},500);
}
addEventListener("keyup", function(e) {
if(e.keyCode == 38) jump()
})
*{
padding:0;
margin:22;
}
#game{
width:500px;
height:500px;
border:1px solid #319b4e;
}
#character{
width:30px;
height:120px;
background-color:green;
position:relative;
top:380px;
border-radius:20px;
/*animation: jump 500ms */
}
/* new class called animate */
.animate{
animation: jump 500ms;
}
#enemy{
width:60px;
height:60px;
background-color:red;
border-radius:14px;
position:relative;
top:320px;
left:440px;
animation: moveenemy 1s infinite linear;
}
@keyframes moveenemy{
0%{left:440px;}
50%{top:58px;}
100%{left:0px; top:320x;}
}
@keyframes jump{
0%{top:380px;}
30%{top:200px;}
50%{top:200px;}
100%{top:380px;}
}
<!DOCTYPE html>
<html lang="en" onclick="jump()">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Game</h1>
<p>My first ever game</p>
<p>We all have an enemy</p>
<div id="game">
<div id="character"></div>
<div id="enemy"></div>
</div>
<script src="script.js"></script>
</body>
</html>
正在工作
问题是:你写 document.getElementbyId("character") 但字符 b 的 getElementbyid 很小所以它通过错误 所以正确的是 document.getElementById("character")
var character = document.getElementById("character");
var enemy = document.getElementById("enemy");
//adding the animate function in the css here, so it is applied to our character
function jump(){
character.classList.add("animate");
setTimeout(function(){
character.classList.remove("animate");
},500);
}
*{
padding:0;
margin:22;
}
#game{
width:500px;
height:500px;
border:1px solid #319b4e;
}
#character{
width:30px;
height:120px;
background-color:green;
position:relative;
top:380px;
border-radius:20px;
/*animation: jump 500ms */
}
/* new class called animate */
.animate{
animation: jump 500ms;
}
#enemy{
width:60px;
height:60px;
background-color:red;
border-radius:14px;
position:relative;
top:320px;
left:440px;
animation: moveenemy 1s infinite linear;
}
@keyframes moveenemy{
0%{left:440px;}
50%{top:58px;}
100%{left:0px; top:320x;}
}
@keyframes jump{
0%{top:380px;}
30%{top:200px;}
50%{top:200px;}
100%{top:380px;}
}
<!DOCTYPE html>
<html lang="en" onclick="jump()">
<head>
<meta charset="UTF-8">
<title>Document</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<h1>Game</h1>
<p>My first ever game</p>
<p>We all have an enemy</p>
<div id="game">
<div id="character"></div>
<div id="enemy"></div>
</div>
<script src="script.js"></script>
</body>
</html>
Is setTimeout an existing inbuilt method, and if so where do we find documentation on these things?
- 是的,
setTimeout
是一种内置方法。您可以在此处了解更多信息 MDN Web Docs.
how the setTimeout function works
- 因此,
setTimeout
方法接受一个 function/code 和一个延迟(以毫秒为单位),之后它将执行它。参考上面link.
它将事件带入事件循环,使其成为异步调用。
稍后我会查看代码并回复您! :D
收藏MDN Web Docs,真的好用!