私はp5jsフレームワークでこのブレークアウトゲームを作っていますが、ボールがブロックに当たっているかどうかを確認するときにちょっと立ち往生しています。私はすべてのブロックを配列内のオブジェクトとして持っています。そして、配列とフレームをループして、ボールと衝突するかどうかをチェックしますが、if文は正しく動作しません。Js if文が正常に動作しない
これは私のループは次のようになります。そのx位置が要件を満たしている場合は、現在のブロックとボールのxとyの位置)
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ていることがわかりますが、私はログインしている場合
block {x: "70", y: "315", c: "white"} 200 470
//I get a lot more results but this is an example.
が、70 + 10 また、私の第二にconsole.log()がトリガされませんので、これはログインしてはならない。それは私はこの結果を得ます。ボールの半径Rと
this.x = x;
this.y = y;
this.r = 10;
:
これは私のボールオブジェクトの変数がどのように見えるかです。私は誰かが役立つことを願って
this.x = x;
this.y = y;
:
これは次のようにブロックオブジェクトが見えるものです。
これは私が今日後で試してみる答えだと思います –
はい、ありがとうございました –