JavaScript跳转功能失败,找不到错误

虚构

我有一个使用JavaScript,CSS和HTML编写简单游戏的开始,直到我创建了跳转功能,游戏一直有效。OnClick时,character元素(由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>

蓝鸟

B“高

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

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

至于setTimeout,它是一个内置函数,在一定时间(在第二个参数中指定)后执行一个函数(在第一个参数中提供),该函数在后台运行,因此不会阻止其他事情的发生。

有关setTimeout的更多信息(当我第一次开始学习html等时,我使用了此网站。)

如果您想将其更改为向上箭头,只需添加一个event listener即可检查keyup事件,并检查键码是否与向上箭头(38)匹配,其他键可以通过console.log为事件选择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>

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

错误:找不到功能

错误:找不到功能“指南”

JavaScript界面找不到功能

cocoapods安装失败,找不到命令错误

测试失败,出现“找不到方法”错误

MSVC错误?找不到功能受限的成员

JavaScript错误:找不到导入的模块

Javascript systemJS错误找不到模块

npm的安装时间失败,显示“找不到”错误

GraphFrame 最短路径失败,找不到 Vertex Id 错误

Devstack安装失败,错误消息为“ XXX:找不到进程”

Android Studio 3.0失败的mergeDebugResources-错误:找不到文件

架构工具失败,错误为“找不到驱动程序”

离子ios构建失败,找不到错误存档

Tensorflow错误:DLL加载失败:找不到指定的过程

RollupJS`解析失败:错误:找不到模块“ babel-runtime”

错误:DEP6200:自举“设备”失败。找不到设备

Pyinstaller:导入错误:DLL加载失败:找不到指定的模块

找不到构建失败Sphinx错误content.rst

致命错误:找不到phpmailerException:SMTP connect()失败

模块构建失败:错误:找不到模块'node-sass'

TEdgeBrowser导航失败,错误为“找不到文件”

Bot创建失败并出现错误:找不到

打字稿错误,找不到名称“ gapi”,翻译失败

ElastAlert 导入错误:DLL 加载失败:找不到指定的程序

配置qtbase失败,并显示错误:“找不到qmake规范”。

Pyinstaller 导入错误:DLL 加载失败:找不到指定的模块

错误:包或名称空间加载失败,找不到对象。

错误:找不到组织或项目:app:bundleReleaseJsAndAssets失败