【发布时间】:2021-05-08 03:28:24
【问题描述】:
我试图在 HTML、CSS 和 JavaScript 游戏中为圆形添加一些重力。相关代码的JavaScript部分如下:
repl.it https://repl.it/join/wukcvzow-iamapersonthing
按照本教程和相关部分在:06.44 分钟。
JavaScript:
var block = document.getElementById("block");
var hole = document.getElementById("hole");
//add this for the setInterval function
var character=document.getElementById("character");
hole.addEventListener('animationiteration',() => {
var random = Math.random()*3;
var top = (random*100)+150;
hole.style.top=-(top) + "px";
});
//interval function runs every 10 milliseconds
setInterval(function(){
var characterTop =
//this is the gravity function
parseInt(window.getComputedStyle(character).getPropertyValue("top"));
character.style.top=(characterTop+3)+"px";
},10);
我假设问题在于最后一点:
},10);
但由于代码没有显示任何语法或其他错误,我无法进一步排除故障。我也玩过 CSS,但似乎找不到问题的根源。
后面的教程建议“10”是刷新率,但我认为不是。那 10 不知何故使球在向下运动中移动得更慢或更快。我想要的是整个动画(球向下移动)保持每 10 秒刷新一次。
简而言之,我希望粉球(角色元素)每 10 秒不断落下。此刻它掉下来一次,然后从屏幕上掉下来。当按下“RUN”时,它才会再次下降。
这个项目的 HTML 是:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>FlappyBird</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div class="sky">
<div class="ground">
<div id="game">
<div id="block"></div>
<div id="hole"></div>
<div id="character">
</div>
</div>
<script src="script.js"></script>
</body>
</html>
还有 CSS
*{
padding:0;
margin:0;
}
#game{
width:400px;
height:500px;
border: 1px solid greenyellow;
margin:auto;
overflow:hidden;
}
#character{
width:50px;
height:50px;
background-color:rgb(255, 0, 149);
position: absolute;
top:250px;
border-radius:50%;
}
#block{
width:50px;
height:500px;
background-color:greenyellow;
position: relative;
left:400px;
animation:block 2s infinite linear;
}
@keyframes block{
0%{left:400px}
100%{left:-50px}
}
#hole{
width:50px;
height:150px;
background-color:white;
position: relative;
left:400px;
top:-500px;
animation:block 2s infinite linear;
}
/*
.sky{
background-color:aqua;
width:400px;
height:500px;
position: relative;
}
.ground{
background-color:brown;
width:400px;
height:100px;
position: relative;
top:500px;
}
*/
【问题讨论】:
-
绿条怎么样?它应该继续以同样的方式滑过吗?
-
这是飞鸟的复制品(已尝试)。早期阶段。所以绿条正在移动,“洞”在绿条中产生间隙。绿条应滑过,球应每 10 秒不断下降。 (重力)
-
不,球不应该每 10 秒落一次。它应该比这更频繁地下降。我相信您真正想要的是它每 10 毫秒下降一次,这是频率的 1000 倍。
-
如果你想复制游戏,为什么不直接使用那家伙的代码作为指导?
-
@Compoot 我相信问题是这里没有人知道问题是什么是。到目前为止,您指出“有用”的唯一内容是阅读教程的建议......您并没有告诉我们您需要什么或想要什么,您只是说“这不是解决方案”所有不是指向您已经拥有的同一教程的链接的东西......?
标签: javascript html css setinterval