我正在尝试制作一款自上而下的射击游戏风格的游戏,让人想起旧的Galaga游戏。 除了当我试图检查玩家和敌方太空船发射的子弹之间的碰撞时,我已经让玩家,敌人和射弹移动并且工作正常。
子弹和敌人都是使用构造函数制作的,然后放在一个数组中以跟踪它们。
//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;
}
}
我试图建立一个碰撞检测器功能,首先通过子弹阵列,然后是敌人阵列并检查重叠的边界,并在发现碰撞时发出警报,但我遇到了麻烦它。如果有人可以提供帮助,我将非常感激。
//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");
}
}
}
}
答案 0 :(得分:0)
你错过了敌人的索引:public static void bomberAlgo(String str)
{
String newString="";
String givenString=str;
for(int i=0;i<givenString.length()-1;i++)
{
if(givenString.charAt(i)!=givenString.charAt(i+1))
{
newString=newString+givenString.charAt(i);
}
}
System.out.println("The new String is "+str);
}
敌人有enemies[e]
enemyX
enemyY
和enemyWidth
属性,enemyHeight
有: bullets
,bulletX
bulletY
bulletWidth
bulletHeight