0
나는 오래된 갈라가 게임을 연상케하는 하향식 슈팅 게임을 만들려고하고있다. 플레이어와 적군과 발사체가 움직이고 잘 작동하고있어 플레이어가 발사 한 총알과 적의 우주선 사이의 충돌을 확인하려고합니다.내 자바 스크립트 충돌 감지에 대한 조언
총알과 적 모두 생성자를 사용하여 생성 된 다음 배열을 추적하여 추적합니다.
//due to the number of enemies on screen they'll be held in an array
var enemies = [];
for(e = 0; e < enemies; e++) {
enemies[e] = [];
enemies[e] = {x:0,y:0};
}
//class constructor to create enemies
class enemy {
constructor(x,y) {
this.enemyX = x;
this.enemyY = y;
this.enemyWidth = 32;
this.enemyHeight = 32;
}
}
//variable array for the bullets
var playerBullets = [];
for(i = 0; i < playerBullets; i++) {
playerBullets[i] = { x: 0, y: 0 };
}
//class constructor to create the bullets
class bullet {
constructor(x,y){
this.bulletX = x;
this.bulletY = y;
this.bulletWidth = 5;
this.bulletHeight = 5;
}
}
나는 제 탄알 배열하고 경계를 오버랩 충돌이 발견되었을 때 경고를 제공하기위한 원수 검사들의 어레이를 통과 충돌 검출 기능을하려고했지만, I '는 그것으로 문제가있다. 누구든지 도와 주시면 대단히 감사하겠습니다. enemies[e]
과 원수가 enemyX
enemyY
enemyWidth
및 enemyHeight
재산 및 bullets
있음 :
//code for detecting collisions from the bullets
function collisionDetection() {
for(i = 0; i < playerBullets.length; i++) {
for(e = 0; e < enemies.length; e++) {
if (playerBullets[i].x < enemies.x + enemy.width &&
playerBullets[i].x + bullet.width > enemies.x &&
playerBullets[i].y < enemies.y + enemy.height &&
playerBullets[i].y + bullet.height > enemies.y){
alert("HIT");
}
}
}
}
https://codereview.stackexchange.com/ – Peter
@Peter 코드가 작동 중일 때 코드를 게시 할 수 있습니다. 그러나 그때까지 OP는 "_I 문제가 있습니다 ._"라고 입력했기 때문에 [주제를 벗어났습니다] (https://codereview.stackexchange.com/help/on-topic) 될 것입니다. –
미안하지만, 총알과 적들이 모두 원 모양이라면 Pythagorean 정리로 중점 거리를 계산할 필요가 있습니다. C가 작 으면 2 원 라디오가 맞습니다. – Peter