2016-12-03 6 views
0

こんにちは私はJavaScriptを使用して自分のCSクラスのゲームを作っています。私は、オブジェクトエールキャンバスに互いに衝突する方法を知っているが、私はそれが別のオブジェクトオブジェクトが完全に他のオブジェクトの内側にあるかどうかを検出するJavaScript

If (object1.xcoord > object2.xcoord 
    && object1.xcoord + object1.width < object2.xcoord + object2.width 
    && object1.ycoord + object1.height < object2.ycoord +object2.height) { 
    alert("hi") 
} 

注内に完全だ場合、それは私には関係ありません私はこれらの3つの側面を必要とし検出するために、オブジェクトを取得しようとしているmはオブジェクト1がオブジェクト2

の上面内にある場合も、それだけで何形状何か

+0

<または>のような比較を使用していないことが可能であることであり、それらの両方が同じ形状でありますか? – Viliami

+0

@Viliamiはどちらも長方形です – user6850954

答えて

2
//both height and width need to be smaller than the containing objects height 
//and width. 
if(obect1.width < object2.width && object1.height < object2.height){ 
    //check if right side x of object1 is less than right side x of object2 
    //and check if bottom y of object1 y is less than bottom y of object2 
    //and check if left x of object1 is greater than left side x of object2 
    //and check if top y of object1 is greater than top y of object2 
    if(object1.x+object1.width < object2.x+object2.width 
    && object1.y+object1.height < object2.y + object2.height 
    && object1.x > object2.x && object1.y > object2.y){ 
     return True; 
    } 
} 
return False; 
+0

ありがとう、あなたのコードは、下側と右側を確認するためだけにチェックします。オブジェクト1がオブジェクト2の左にあってもコードはtrueを返します – user6850954

関連する問題