<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title></title>
<style type="text/css">
.redDiv{
width: 400px;
height: 400px;
background-color: greenyellow;
position: relative;
margin: 100px auto;
}
.yellow{
width: 50px;
height: 50px;
background-color: yellow;
position: absolute;
}
</style>
</head>
<body>
<div class="redDiv"></div>
<div class="yellow"></div>
</body>
<script type="text/javascript">
var redDiv=document.querySelector(".redDiv");
var yellow= document.querySelector(".yellow");
yellow.onmousedown = function (ev) {
var x=ev.offsetX;
var y=ev.offsetY;
document.onmousemove = function (e) {
yellow.style.left=e.clientX-x+'px';
yellow.style.top=e.clientY-y+'px';
// 碰撞检测
// 1.蓝右>红左
// 2.蓝顶<红底
// 3.蓝底>红顶
// 4.蓝左<红右
if(yellow.offsetLeft+yellow.offsetWidth>=redDiv.offsetLeft && yellow.offsetTop<=redDiv.offsetTop+redDiv.offsetHeight && yellow.offsetTop+yellow.offsetHeight>=redDiv.offsetTop && yellow.offsetLeft<=redDiv.offsetLeft+redDiv.offsetWidth){
console.log("碰到我了");
}
}
}
yellow.onmouseup = function () {
document.onmousemove= function () {
}
}
</script>
</html>
检测碰撞
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
相关阅读更多精彩内容
- 我们把需要检测碰撞的物理设置物理体(以下我们把这个物理体称作xx) xx.physicsBody=SKPhysic...
- 在我们的游戏开发过程中,有一个很重要的工作就是进行碰撞检测。例如在射击游戏中子弹是否击中敌人,在RPG游戏中是否捡...