出于某种原因,curAlien和battleShip之间的碰撞不会发生。这是他们的代码片段。我无法决定使用boolean collisionWithPlayer,或使用访问器,(如果我使用访问器,我会收到错误说明(ax无法解析或不是字段)
for (i = 0; i < alienHorde.size (); i++)
{
Alien curAlien = alienHorde.get(i);
curAlien.act();
if (battleShip.collisionWithPlayer(curAlien))
// if ((badAlien.getX().ax + alienWidth) == battleShip.getX().sx && badAlien.getX().ax <= (battleShip.get().sx + shipWidth) &&
//(badAlien.getY().ay + alienHeight) == battleShip.getY().sy && badAlien.getY().ay <= (battleShip.getY().sy + shipHeight))
{
health=health-10;
alienHorde.remove(i);
}
这是完整的代码
Ship battleShip;
Alien badAlien;
ArrayList <Alien> alienHorde;
ArrayList <Missile> missiles;
int firstAlienX = 30;
int firstAlienY = -65;
final int alienOffset = 110;
final int alienWidth = 64;
final int alienHeight = 80;
final int shipWidth = 64;
final int shipHeight = 64;
PImage alien;
PImage ship;
PImage missile;
int health = 100;
int bCount = 0;
//int enemies = 0;
int b;
int i;
void setup()
{
size(1000, 500);
ship = loadImage("ship.png");
alien = loadImage("ghost.png");
missile = loadImage("missile.gif");
battleShip = new Ship();
frameRate(1000);
missiles = new ArrayList<Missile>();
alienHorde = new ArrayList<Alien>();
for (int a = 0; a<12; a++)
{
alienHorde.add(new Alien(firstAlienX + a * alienOffset, firstAlienY));
//enemies++;
}
}
void mousePressed()
{
missiles.add(new Missile());
//bCount++;
}
void draw()
{
background(0);
battleShip.draw();
for (Alien badAlien : alienHorde)
{
badAlien.act();
badAlien.draw();
}
for (i = 0; i < alienHorde.size (); i++)
{
Alien curAlien = alienHorde.get(i);
curAlien.act();
if (battleShip.collisionWithPlayer(curAlien))
// if ((badAlien.getX().ax + alienWidth) == battleShip.getX().sx && badAlien.getX().ax <= (battleShip.get().sx + shipWidth) &&
//(badAlien.getY().ay + alienHeight) == battleShip.getY().sy && badAlien.getY().ay <= (battleShip.getY().sy + shipHeight))
{
health=health-10;
alienHorde.remove(i);
}
for (b = 0; b < missiles.size (); b++)
{
Missile curMissile = missiles.get(b);
curMissile.act();
if (curMissile.collides(alienHorde.get(i)))
{
alienHorde.remove(i);
missiles.remove(b);
i--;
}
}
}
text("Health: " + health, 20, 65);
}
public class Alien
{
int ax;
int ay;
int xVelocity;
int yVelocity;
Alien(int axIn, int ayIn)
{
ax = axIn;
ay = ayIn;
xVelocity = -1;
yVelocity = 0;
draw();
}
void draw()
{
image(alien, ax, ay, 64, 64);
}
void act()
{
ax+=xVelocity;
if (ax+alienWidth >= width || ax < 0)
{
xVelocity*=-1;
ay=ay+alienHeight;
}
}
int getX()
{
return ax;
}
int getY()
{
return ay;
}
}
public class Missile
{
int mx;
int my;
int yVelocity = -1;
Missile()
{
mx = mouseX-15;
my = 330;
draw();
}
void draw()
{
image(missile, mx, my, 30, 70);
}
void act()
{
my+=yVelocity;
draw();
}
boolean collides(Alien x)
{
if ( mx >= x.ax && mx <= x.ax + alienWidth && my >= x.ay && my <= x.ay + alienHeight)
return true;
else
return false;
}
}
public class Ship
{
int sx = 30;
int sy = 75;
Ship()
{
draw();
}
void draw()
{
image(ship, mouseX-32, 400, 64, 64);
}
/*
int getX()
{
return sx;
}
int getY()
{
return sy;
}
*/
boolean collisionWithPlayer(Alien aenemy)
{
if ((aenemy.ax + alienWidth) == sx && aenemy.ax <= (sx + shipWidth)
&& (aenemy.ay + alienHeight) == sy && aenemy.ay <= (sy + shipHeight))
{
return true;
}
return false;
}
}