敌人破坏slick2d

时间:2012-10-23 03:21:31

标签: java slick2d

我目前正在使用Slick2D在java中处理一种平台游戏,以及我目前设置它的方式,敌人是ArrayList中的敌人对象。玩家可以向他们射击小圆圈(也包含在ArrayList中),如果他们连接,应该发生的是敌人“死亡”。这是我遇到麻烦的地方。我如何摆脱这样的对象?我已经尝试在每个循环中从数组中删除它们,但由于它仍在搜索所述数组(我认为),因此抛出异常。

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();
    }

}

}

3 个答案:

答案 0 :(得分:1)

正如您所指出的,您不能使用for-each循环修改某些内容并继续迭代。相反,您需要使用Iteratorsee here

由于ArrayList中的删除还需要将删除的对象后的每个对象左移一个索引(因此是O(n)操作),我建议您也可以查看使用LinkedList代替。

关于样式,您还应始终通过其界面引用Collection个对象,而不是它们的实现。也就是说,

ArrayList<Integer> eRemove = new ArrayList<Integer>();

应该是

List<Integer> eRemove = new ArrayList<Integer>();

这样,如果您决定更改为LinkedList,则只需将代码更改为:

List<Integer> eRemove = new LinkedList<Integer();

答案 1 :(得分:0)

是的,尝试从当前正在迭代的ArrayList中删除元素必然会导致问题。

你确定你的'交叉'代码有效吗?快速查看替换方法是您需要的。如果它没有做任何事情,也许它找不到交叉点。

答案 2 :(得分:0)

您当前的代码应该至少删除第一个子弹和第一个敌人。如果没有,则intersects方法存在问题。

当第一颗子弹被移除后,它后面的所有元素都会移动,所以你会删除错误的子弹。

试试这个:

for (int i = 0; i < bullets.size(); i++) {
        boolean hit = false;
        for (int z = 0; z < enemies.size(); z++) {

            if (bullets.get(i).intersects(enemies.get(z).getPoly())) {
                hit = true;
                enemies.remove(z);
                z--;
            }
        }

        if (hit) {
            bullets.remove(i);
            i--;
        }
}

你应该担心ArrayList和LinkedList的性能,直到你有成千上万的子弹和敌人。