所以我正在用p5js框架做这个突破游戏,但是在检查球是否碰到块时我有点卡住了。我把所有的块都作为一个数组中的一个对象,每循环遍历数组来检查它是否与球碰撞,但不知何故我的if语句无法正常工作。Js if语句无法正常工作
这是我的循环是什么样子:
for(var i=0; i<blocks.length; i++){
if(this.x <= (blocks[i].x + 10) && this.x >= (blocks[i].x - 10)){
//the + and - 10 stand for the length of the block which is 20 and is drawn from the center
console.log(blocks[i], this.x, this.y);
if(this.y <= blocks[i].y + 10 && this.y >= blocks[i].y + 5){
//here the + 10 stands for the height of the block/2 + the height of the ball/2 and the + 5 stands for the height of the height of the block/2
console.log('yes');
}
}
}
你可以看到,我CONSOLE.LOG()当前块和球x和y的位置,如果它的x位置是否符合要求,但如果我登录它,我得到这样的结果:
block {x: "70", y: "315", c: "white"} 200 470
//I get a lot more results but this is an example.
,但是这不应该被记录,因为70 + 10 也是我第二次的console.log()永远不会触发。
这是我的球对象的变量是这样的:
this.x = x;
this.y = y;
this.r = 10;
其中r为球的半径。
这是块对象是什么样子:
this.x = x;
this.y = y;
我希望有人可以提供帮助。
谢谢我认为这是答案我会在今天晚些时候尝试它 –
是的,它的工作非常感谢你 –