【问题标题】:JavaScript jump function failing, cannot find error [closed]JavaScript跳转功能失败,找不到错误[关闭]
【发布时间】:2021-02-04 10:41:46
【问题描述】:

我开始使用 JavaScript、CSS 和 HTML 编写一个简单的游戏,并且在我创建了一个跳转函数之前,该游戏一直在运行。 OnClick,字符元素(由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>

【问题讨论】:

  • 它将是getElementById 而不是getElementbyId
  • 谢谢!想知道为什么编辑不选择这个并标记....解决了,谢谢
  • @Compoot 是的,我想知道他们为什么关闭它,对于 setTimeout 等仍然有效的问题,并且它适合初学者问...
  • 非常感谢您的帮助 - 谢谢!
  • @bluejayke - 如果你有兴趣,我的同事(和我一样)也发布了这个! stackoverflow.com/questions/66048374/…

标签: javascript html css settimeout


【解决方案1】:

在复制代码并对其进行测试之后,问题在于术语 document.getElementById 用小写字母 b 拼写,如 document.getElementbyId,这两行应改为:

var character = document.getElementById("character");
var enemy = document.getElementById("enemy");`

至于setTimeout,它是一个内置函数,在一定时间(第二个参数指定)后执行一个函数(第一个参数提供),这个函数在后台工作,所以它不会t 阻止其他事情发生。

For more information on setTimeout(我刚开始学习html等的时候用过这个网站……)

如果您想将其更改为向上箭头,只需添加 event listener 以检查 keyup 事件,并检查键码是否与向上箭头 (38) 匹配,并且可以通过以下方式检查其他键console.loging 事件的 keyCode,所以在你的 JavaScript 中的某个地方,做

addEventListener("keyup", function(e) {
    if(e.keyCode == 38) jump()
})

所以...

整个sn-p是

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>

【讨论】:

    【解决方案2】:

    现在工作

    问题是:你写的 document.getElementbyId("character") 但是 getElementbyid 的字符 b 很小所以它通过错误 所以正确的是 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>

    【讨论】:

      【解决方案3】:

      setTimeout 是现有的内置方法吗?如果是,我们在哪里可以找到有关这些内容的文档?

      • 是的,setTimeout 是一种内置方法。你可以在这里MDN Web Docs了解更多信息。

      setTimeout 函数的工作原理

      • 因此,setTimeout 方法接受一个函数/代码和一个延迟(以毫秒为单位),之后它将执行它。参考上面的链接。

      它将事件带入事件循环,使其异步调用。

      我会在一段时间后查看代码并回复您! :D

      收藏 MDN Web Docs,真的很方便!

      【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-20
      • 2012-08-04
      • 1970-01-01
      • 2015-12-18
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      相关资源
      最近更新 更多