一个js判断检测的问题

dilongzuyj 2024-02-06 19:40:11

如题,碰撞检测的问题

在调试时用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>

 

...全文
486 4 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
太空漫步11 2024-02-07
  • 打赏
  • 举报
回复

go()函数里面的if条件

if(y < 500){
    if(!onroad){
      y += dy;
    }
  }

!onload是不是要改成onload

dilongzuyj 2024-07-23
  • 举报
回复
@太空漫步11 不应该,onroad指在轨道上,为什么还会下落呢
r00_a2lBUR 2024-02-07
  • 打赏
  • 举报
回复

我想这个是游戏网页

dilongzuyj 2024-02-07
  • 举报
回复
@r00_a2lBUR awa,差不多,游戏引擎,可以编写很多的闯关跑酷游戏

30,439

社区成员

发帖
与我相关
我的任务
社区描述
就等你来冒个泡~
社区管理员
  • 灌水乐园
  • 社区助手
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

版主:

社区助手

 

试试用AI创作助手写篇文章吧