30,439
社区成员




如题,碰撞检测的问题
在调试时用console.log发现最后一直是true
所以掉不下来
awa,能帮我康康错在哪儿吗
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<style>
#abo {
background-color:#65C4FF;
width:1000px;
height:500px;
}
#ball {
background-color:#F1B;
border-radius:30px;
width:30px;
height:30px;
position:absolute;
left:20px;
top:200px;
}
</style>
</head>
<body>
<div id="abo">
<div id="ball"></div>
</div>
<script>
var run;
var ball = document.getElementById("ball");
var area = document.getElementById("abo");
var dy = 1;
var y = 0;
var ditu =
[
[1,1,1],
[1,0,1],
[1,0,1],
[1,0,1],
[1,0,1],
[1,0,0],
[1,1,1],
[1,1,1],
[1,1,1],
[0,1,1],
[0,1,1],
[0,1,1],
[0,1,1],
[0,1,1],
[1,0,1],
[1,1,1],
[1,1,1],
[1,1,1],
[1,1,0],
[1,1,0],
[1,1,0],
[1,1,0],
[0,1,0],
[0,0,0],
[0,0,1],
[0,1,0],
[1,0,0],
[1,0,1],
[1,1,1],
[1,1,1],
[1,1,1],
[1,1,0],
[1,1,1],
[1,1,1],
[1,1,1],
[0,1,1],
[1,1,1],
[1,1,1],
[1,1,1]
];
var canJump = true;
var roads = [];
var onroad = false;
var orset = [];
var ter;
document.documentElement.addEventListener("click", function(){
if(canJump){
dy = -1.5;
}
});
function go(){
if(dy < 1){
canJump = false;
dy += 0.01;
}else{
dy = 1;
setTimeout(function(){canJump = true;}, 205);
}
ter.style.left = `${ter.offsetLeft - 1}px`;
if(ter.offsetLeft >= 1000){
ter.style.visibility = "hidden";
}else{
ter.style.visibility = "visible";
}
if(ter.offsetLeft - 20 <= 30){
clearInterval(run);
let r = confirm("你胜利了,是否退出游戏");
if(r){
close();
}else{
tru();
}
}
roads.forEach(gtg =>{
let rx = gtg.offsetLeft;
let ry = gtg.offsetTop;
orset.push(rx - 20 >= -30 && 20 - rx >= -30 && ry - y >= 30 && ry - y >= -30);
if(rx >= 1000){
gtg.style.visibility = "hidden";
}else if(rx <= 0){
gtg.remove();
}else{
gtg.style.visibility = "visible";
}
gtg.style.left = `${rx - 1}px`;
});
console.clear();
console.log(orset);
orset.forEach(gtg =>{
if(gtg){
onroad = true;
}
});
orset = [];
if(y < 500){
if(!onroad){
y += dy;
}
}else{
y = 500;
clearInterval(run);
let r = confirm("你失败了,是否退出游戏");
if(r){
close();
}else{
tru();
}
}
ball.style.top = `${y}px`;
}
function tru(){
roads.forEach(gthi =>{
gthi.remove();
});
roads = [];
if(ter != undefined){
ter.remove();
}
ditu.forEach((gth, ine) =>{
gth.forEach((gthe, index) =>{
if(gthe >= 1){
let div = document.createElement("div");
div.setAttribute("style", `background-color:#FFDE;width:30px;height:30px;position:absolute;top:${(index + 1)* 50 + index * 30 + 125}px;left:${ine * 30 + 20}px;`);
area.appendChild(div);
roads.push(div);
}
});
if(ine == ditu.length - 1){
let div = document.createElement("div");
div.setAttribute("style", `background-color:#FF7;width:30px;height:500px;position:absolute;top:0px;left:${ine * 30 + 50}px;`);
area.appendChild(div);
ter = div;
div.setAttribute("id", "terminal");
}
});
x = 20;
y = 250;
run = setInterval(go, 1);
}
tru();
</script>
</body>
</html>
go()函数里面的if条件
if(y < 500){
if(!onroad){
y += dy;
}
}
!onload是不是要改成onload
我想这个是游戏网页