让对象检测是否完全在其他对象JavaScript中 [英] Have object detect if completely inside other object JavaScript
本文介绍了让对象检测是否完全在其他对象JavaScript中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
您好,我正在使用JavaScript为CS课制作游戏。我知道如何处理对象在画布中彼此碰撞,但是我试图获取一个对象以检测其是否完全在另一个对象内
Hello I'm making a game for my CS class using JavaScript. I know how to ale objects collide with each other in the canvas but I m trying to get an object to detect if it's completely inside another object
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的顶部之内,对我来说就没关系了
Note I only need those three sides it doesn't matter to me if object 1 is within top side of object 2
也可以只使用<或>而不是其他任何东西
Also is it possible to only use comparisons like < or > and not anything else
推荐答案
//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;
这篇关于让对象检测是否完全在其他对象JavaScript中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文