我目前正在使用 Slick2D 和我目前设置它的方式在 java 中开发一种平台游戏,敌人是 ArrayList 中的 Enemy 对象。玩家可以向它们射击小圆圈(也包含在 ArrayList 中),如果它们连接起来,应该发生的是敌人“死亡”。这就是我遇到麻烦的地方。我该如何摆脱这样的物体?我已经尝试在 for each 循环中将它们从数组中删除,但是由于它仍在搜索所述数组(我认为),这会引发异常。
for (Bullet bullet : bullets) {
for (Enemy enemy : enemies) {
if (bullet.intersects(enemy) {
bullets.remove(bullet);
enemies.remove(enemies);
}
}
}
那没有用,所以现在我试图制作一个子弹和敌人的索引数组,然后在循环后删除它们,如下所示:
for (int i = 0; i < bullets.size(); i++) {
for (int z = 0; z < enemies.size(); z++) {
if (bullets.get(i).intersects(enemies.get(z).getPoly())) {
bRemove.add(i);
eRemove.add(z);
}
}
}
for (Integer i : bRemove) {
bullets.remove(i);
}
for (Integer i : eRemove) {
enemies.remove(i);
}
现在似乎什么都没有发生。有谁知道我应该怎么做?这是主类的全部内容:
package ca.wmc.BasicPlatform;
import java.lang.Math;
import java.util.ArrayList;
import org.newdawn.slick.Animation;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.geom.Circle;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Input;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.SpriteSheet;
import org.newdawn.slick.geom.Polygon;
public class Game extends BasicGame {
Player quote;
public BlockMap map;
private boolean start;
private int startCount;
private boolean lost;
private int lostCount;
private int hpTime = 0;
private ArrayList<Enemy> enemies;
private ArrayList<Bullet> bullets;
private int reload = 0;
private final double a = 2;
private int t = 0;
public Game() {
super("Super Simple Platform");
}
public void init(GameContainer container) throws SlickException {
container.setVSync(true);
container.setTargetFrameRate(60);
map = new BlockMap("tiled/newmap.tmx");
quote = new Player("tiled/Quote.gif", 320, 320, 3, -8);
enemies = new ArrayList<Enemy>();
enemies.add(new Enemy("tiled/King.gif", 50, 0, -8, 26, 32));
enemies.add(new Enemy("tiled/Quote.gif", 200, 150, -8, 28, 32));
start = false;
lost = false;
bullets = new ArrayList<Bullet>();
}
public void update(GameContainer container, int delta) throws SlickException {
if (!start) {
if (container.getInput().isKeyDown(Input.KEY_Z)) {
start = true;
}
} else {
if (!lost) {
ArrayList<Integer> eRemove = new ArrayList<Integer>();
ArrayList<Integer> bRemove = new ArrayList<Integer>();
quote.update(container, a, t, delta);
for (Enemy enemy : enemies) {
enemy.update(quote, a, t);
}
if (t == 5) {
t = 0;
} else {
t++;
}
if (container.getInput().isKeyDown(Input.KEY_X) && reload == 0) {
if (quote.getCurAnimation() == quote.right()) {
bullets.add(new Bullet(quote.getXPos() + 28, quote.getYPos() + (int)(20 * Math.random() - 22) + 25, "r"));
} else {
bullets.add(new Bullet(quote.getXPos() + 28, quote.getYPos() + (int)(20 * Math.random() - 22) + 25, "l"));
}
reload = 10;
}
if (reload > 0) {
reload--;
}
for (Bullet bullet : bullets) {
if (bullet.getDirection()) {
bullet.setCenterX(bullet.getCenterX() + 10);
} else {
bullet.setCenterX(bullet.getCenterX() - 10);
}
}
for (Enemy enemy : enemies) {
if (quote.getPoly().intersects(enemy.getPoly()) && hpTime > 50 && quote.getHP() != 0) {
quote.setHP(quote.getHP() - 1);
hpTime = 0;
if (quote.getHP() <= 0) {
lost = true;
}
}
}
hpTime++;
if (startCount <= 100) {
startCount++;
}
for (int i = 0; i < bullets.size(); i++) {
for (int z = 0; z < enemies.size(); z++) {
if (bullets.get(i).intersects(enemies.get(z).getPoly())) {
bRemove.add(i);
eRemove.add(z);
}
}
}
for (Integer i : bRemove) {
bullets.remove(i);
}
for (Integer i : eRemove) {
enemies.remove(i);
}
}
if (quote.getYPos() > 640) {
lost = true;
}
}
}
public void render(GameContainer container, Graphics g) throws SlickException{
g.drawImage(new Image("apsci.jpg"), 0, 0);
BlockMap.tmap.render(0, 0);
g.drawAnimation(quote.getCurAnimation(), quote.getXPos(), quote.getYPos());
for (Enemy enemy : enemies) {
g.drawAnimation(enemy.getCurAnimation(), enemy.getXPos(), enemy.getYPos());
}
for (Circle bullet : bullets) {
g.draw(bullet);
}
g.drawString("HP: " + quote.getHP(), 10, 450);
if (!start) {
g.drawString(getTitle(), 150, 0);
g.drawString("Controls: arrow keys to move, Z to jump, X to fire.", 150, 50);
g.drawString("Defeat the evil. Press Z to start.", 150, 100);
}
if (startCount < 100) {
for (Enemy enemy : enemies) {
g.drawString("The Evil -->", enemy.getXPos() - 125, enemy.getYPos() + 10 + (int)(2 * Math.random()));
}
g.drawString("<-- You", quote.getXPos() + 50, quote.getYPos() + 10 + (int)(2 * Math.random()));
}
if (lost) {
g.drawString("You have lost.", 200, 210);
}
if (lostCount > 150) {
g.drawString("Game over.", 200, 260);
}
if (lostCount > 200) {
g.drawString("If that wasn't already obvious.", 200, 310);
}
}
public static void main(String[] args) throws SlickException{
// TODO Auto-generated method stub
try {
AppGameContainer app = new AppGameContainer(new Game(), 640, 480, false);
app.start();
} catch (SlickException e) {
e.printStackTrace();
}
}
}