2016-12-03 221 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") 
} 

注意里面我只需要这三个面并不重要,我如果对象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