原生js實(shí)現(xiàn)碰撞檢測(cè)
本文實(shí)例為大家分享了js實(shí)現(xiàn)碰撞檢測(cè)的具體代碼,供大家參考,具體內(nèi)容如下
隨手寫(xiě)了個(gè)簡(jiǎn)單的碰撞檢測(cè)的代碼。檢測(cè)box1和box2是否發(fā)生碰撞,若發(fā)生碰撞,box2顏色發(fā)生隨機(jī)改變,并反彈到隨機(jī)位置。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
<style>
.box1,.box2{
width: 100px;
height: 100px;
background-color: #f00;
position:absolute;
}
.box2{
background-color: #00f;
left: 200px;
top: 200px;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
</body>
<script>
var box1=document.querySelector(".box1");
var box2=document.querySelector(".box2");
box1.addEventListener("mousedown",mouseHandler);
function mouseHandler(e){
if(e.type==="mousedown"){
e.preventDefault();
document.elem=this;
document.pointX= e.offsetX;
document.pointY= e.offsetY;
document.addEventListener("mousemove",mouseHandler);
this.addEventListener("mouseup",mouseHandler);
}else if(e.type==="mousemove"){
this.elem.style.left= e.x-this.pointX+"px";
this.elem.style.top= e.y-this.pointY+"px";
hitText(this.elem,box2);
}else if(e.type==="mouseup"){
document.removeEventListener("mousemove",mouseHandler);
this.removeEventListener("mouseup",mouseHandler);
}
}
function hitText(elem1,elem2){
var rect1=elem1.getBoundingClientRect();
var rect2=elem2.getBoundingClientRect();
var ponit1={x:rect1.x,y:rect1.y};
var ponit4={x:rect1.x+rect1.width,y:rect1.y+rect1.height};
if(
ponit4.x>rect2.x
&&ponit1.x<(rect2.x+rect2.width)
&&ponit4.y>rect2.y
&&ponit1.y<(rect2.y+rect2.height)){
elem2.style.backgroundColor=randomColor();
elem2.style.left=Math.round(Math.random()*document.documentElement.clientWidth)+"px";
elem2.style.top=Math.round(Math.random()*document.documentElement.clientHeight)+"px";
}
}
function randomColor(){
var a=Math.round(Math.random()*255);
var b=Math.round(Math.random()*255);
var c=Math.round(Math.random()*255);
var color="rgb("+ a+","+b+","+c+")";
return color;
}
</script>
</html>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- JS實(shí)現(xiàn)碰撞檢測(cè)的方法分析
- javascript制作游戲開(kāi)發(fā)碰撞檢測(cè)的封裝代碼
- js實(shí)現(xiàn)碰撞檢測(cè)特效代碼分享
- 原生JS實(shí)現(xiàn)的碰撞檢測(cè)功能示例
- javascript實(shí)現(xiàn)多邊形碰撞檢測(cè)
- 基于javascript實(shí)現(xiàn)碰撞檢測(cè)
- js實(shí)現(xiàn)拖拽與碰撞檢測(cè)
- 原生js實(shí)現(xiàn)移動(dòng)小球(碰撞檢測(cè))
- js 實(shí)現(xiàn)碰撞檢測(cè)的示例
- three.js利用射線Raycaster進(jìn)行碰撞檢測(cè)
- js實(shí)現(xiàn)碰撞檢測(cè)
相關(guān)文章
給localStorage設(shè)置一個(gè)過(guò)期時(shí)間的方法分享
我們都知道localStorage不主動(dòng)刪除,永遠(yuǎn)不會(huì)銷毀,那么如何設(shè)置localStorage的過(guò)期時(shí)間呢?下面這篇文章主要給大家介紹了關(guān)于如何給localStorage設(shè)置一個(gè)過(guò)期時(shí)間的相關(guān)資料,需要的朋友可以參考下2018-11-11
JS使用插件cryptojs進(jìn)行加密解密數(shù)據(jù)實(shí)例
這篇文章主要介紹了JS使用插件cryptojs進(jìn)行加密解密數(shù)據(jù),結(jié)合完整實(shí)例形式分析了javascript基于加密插件實(shí)現(xiàn)加密解密功能的相關(guān)操作技巧,需要的朋友可以參考下2017-05-05
JavaScript實(shí)現(xiàn)隨機(jī)點(diǎn)名器實(shí)例詳解
這篇文章主要介紹了JavaScript隨機(jī)點(diǎn)名器,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-05-05
小程序?qū)崿F(xiàn)抽獎(jiǎng)動(dòng)畫(huà)
這篇文章主要為大家詳細(xì)介紹了小程序?qū)崿F(xiàn)抽獎(jiǎng)動(dòng)畫(huà),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-12-12

