2016-12-03 9 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