我正在制作一个小 Java 游戏以供练习。由于改变了精灵,我遇到了敌人和射弹之间的碰撞问题。我将提供 Projectile 类,如果有人想看,我可以提供更多!在这里我将非常感谢您的帮助,因为我已经花了几个小时试图解决这个问题。基本上每个敌人都应该受到 5 次打击然后消失,但我似乎无法做到这一点。我认为问题在范围内,但我现在真的不知道发生了什么。dn 和 dn2 是敌人的精灵。r 是我的角色发射的弹丸
public class Projectile {
private int x, y, speedX;
private boolean visible;
private Rectangle r;
public Projectile(int startX, int startY) {
x = startX;
y = startY;
speedX = 7;
visible = true;
r = new Rectangle(0, 0, 0, 0);
}
public void update() {
x += speedX;
r.setBounds(x, y, 10, 5);
if (x > 800) {
visible = false;
r = null;
}
if (x < 800) {
checkCollision();
}
}
private void checkCollision() {
if (r.intersects(StartingClass.dn.r)) {
visible = false;
if (StartingClass.dn.health > 0) {
StartingClass.dn.health -= 1;
}
if (StartingClass.dn.health == 0) {
StartingClass.dn.setCenterX(-100);
StartingClass.score += 5;
}
}
if (r.intersects(StartingClass.dn2.r)) {
visible = false;
if (StartingClass.dn2.health > 0) {
StartingClass.dn2.health -= 1;
}
if (StartingClass.dn2.health == 0) {
StartingClass.dn2.setCenterX(-100);
StartingClass.score += 5;
}
}
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSpeedX() {
return speedX;
}
public boolean isVisible() {
return visible;
}
public void setX(int x) {
this.x = x;
}
public void setY(int y) {
this.y = y;
}
public void setSpeedX(int speedX) {
this.speedX = speedX;
}
public void setVisible(boolean visible) {
this.visible = visible;
}
}
public class Enemy {
private int power, centerX, speedX, centerY;
private Background bg = StartingClass.getBg1();
private Ninja ninja = StartingClass.getNinja();
public static boolean hit = false;
public Rectangle r = new Rectangle(0, 0, 0, 0);
public int health = 5;
private int movementSpeed;
// Behavioral Methods
public void update() {
follow();
centerX += speedX;
speedX = bg.getSpeedX() * 5 + movementSpeed;
r.setBounds(centerX - 25, centerY - 25, 50, 60);
if (r.intersects(Ninja.yellowRed)) {
checkCollision();
}
}
private void checkCollision() {
if (r.intersects(Ninja.rect) || r.intersects(Ninja.rect2) || r.intersects(Ninja.rect3) || r.intersects(Ninja.rect4)){
System.out.println("collision");
hit = true;
}
}
public void follow() {
if (centerX < -95 || centerX > 810) {
movementSpeed = 0;
}
else if (Math.abs(ninja.getCenterX() - centerX) < 5) {
movementSpeed = 0;
}
else {
if (ninja.getCenterX() >= centerX) {
movementSpeed = 1;
} else {
movementSpeed = -1;
}
}
}
public class Ninja {
// Constants are Here
final int JUMPSPEED = -20;
final int MOVESPEED = 5;
private int centerX = 100;
private int centerY = 377;
private boolean jumped = false;
private boolean movingLeft = false;
private boolean movingRight = false;
private boolean ducked = false;
private boolean readyToFire = true;
private int speedX = 0;
private int speedY = 0;
public static Rectangle rect = new Rectangle(0, 0, 0, 0);
public static Rectangle rect2 = new Rectangle(0, 0, 0, 0);
public static Rectangle rect3 = new Rectangle(0, 0, 0, 0);
public static Rectangle rect4 = new Rectangle(0, 0, 0, 0);
public static Rectangle yellowRed = new Rectangle(0, 0, 0, 0);
public static Rectangle footleft = new Rectangle(0, 0, 0, 0);
public static Rectangle footright = new Rectangle(0, 0, 0, 0);
private Background bg1 = StartingClass.getBg1();
private Background bg2 = StartingClass.getBg2();
public Rectangle r = new Rectangle(0, 0, 0, 0);
private ArrayList<Projectile> projectiles = new ArrayList<Projectile>();
public void update() {
// Moves Character or Scrolls Background accordingly.
if (speedX < 0) {
centerX += speedX;
}
if (speedX == 0 || speedX < 0) {
bg1.setSpeedX(0);
bg2.setSpeedX(0);
}
if (centerX <= 200 && speedX > 0) {
centerX += speedX;
}
if (speedX > 0 && centerX > 200) {
bg1.setSpeedX(-MOVESPEED / 5);
bg2.setSpeedX(-MOVESPEED / 5);
}
// Updates Y Position
centerY += speedY;
// Handles Jumping
speedY += 1;
if (speedY > 3) {
jumped = true;
}
// Prevents going beyond X coordinate of 0
if (centerX + speedX <= 60) {
centerX = 61;
}
rect.setRect(centerX - 34, centerY - 30, 67, 62);
rect2.setRect(rect.getX(), rect.getY() + 63, 68, 63);
rect3.setRect(rect.getX() - 26, rect.getY() + 32, 26, 20);
rect4.setRect(rect.getX() + 68, rect.getY() + 32, 26, 20);
yellowRed.setRect(centerX - 100, centerY - 110, 180, 180);
footleft.setRect(centerX - 50, centerY + 20, 50, 15);
footright.setRect(centerX, centerY + 20, 50, 15);
}
public void moveRight() {
if (ducked == false) {
speedX = MOVESPEED;
}
}
public void moveLeft() {
if (ducked == false) {
speedX = -MOVESPEED;
}
}
public void stopRight() {
setMovingRight(false);
stop();
}
public void stopLeft() {
setMovingLeft(false);
stop();
}
private void stop() {
if (isMovingRight() == false && isMovingLeft() == false) {
speedX = 0;
}
if (isMovingRight() == false && isMovingLeft() == true) {
moveLeft();
}
if (isMovingRight() == true && isMovingLeft() == false) {
moveRight();
}
}
public void jump() {
if (jumped == false) {
speedY = JUMPSPEED;
jumped = true;
}
}
public void shoot() {
if (readyToFire) {
Projectile p = new Projectile(centerX + 40, centerY + 80);
projectiles.add(p);
}
}