我遇到一个js触发碰撞失效的问题,疑似问题出现在onroad =(20 - rx <= 30 && 20 - rx > -30 && y - ry <= 30 && y - ry < -30);
和if(ter.style.left - 20 <= 30){
里
<!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,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 ter;
document.documentElement.addEventListener("click", function(){
if(canJump){
dy = -1.5;
}
});
function go(){
console.clear();
ter.style.left = `${parseFloat(ter.style.left)- 1}px`;
if(ter.style.left - 20 <= 30){
clearInterval(run);
let r = confirm("你胜利了,是否退出游戏");
if(r){
close();
}else{
tru();
}
}
roads.forEach(gtg =>{
let rx = parseFloat(gtg.style.left);
let ry = parseFloat(gtg.style.top);
onroad =(20 - rx <= 30 && 20 - rx > -30 && y - ry <= 30 && y - ry < -30);
if(rx <= 0){
gtg.style.display = "none";
}else if(x >= 1000){
gtg.style.display = "none";
}else{
gtg.style.display = "block";
}
gtg.style.left = `${rx - 1}px`;
});
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`;
if(dy < 1){
canJump = false;
dy += 0.01;
}else{
dy = 1
setTimeout(function(){canJump = true}, 205);
}
}
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>