gpt4 book ai didi

未从 LinkedList 中删除 Java 项目

转载 作者:行者123 更新时间:2023-11-29 03:07:58 26 4
gpt4 key购买 nike

我正在开发一个简单的视频游戏,我很快就遇到了一个我无法解决的问题......我的游戏中有很多敌人,所以他们当然都继承自一个类 Enemy 。但是当玩家和敌人发生碰撞时,敌人并没有被移除......这是我的EntityBlueEnemyController敌人。我假设我的 Player 类没问题,因为我的玩家在碰撞时消失了。

实体.java

package com.darksunproductions.ld47.Entities;

import java.awt.Point;
import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import com.darksunproductions.ld47.Game.Game;

public class Entity {

private int x;
private int y;
private Point pos;
private double velX;
private double velY;
private BufferedImage icon;
private boolean dead = false;
private int updatesSinceDeath = -1;

protected Game game;

protected Entity(int x, int y, double velX, double velY, Game game, BufferedImage icon){
this.x = x;
this.y = y;
this.velX = velX;
this.velY = velY;
this.game = game;
this.icon = icon;
}

public void setUpdatesSinceDeath(int i){
updatesSinceDeath = i;
}
public int getUpdatesSinceDeath(){
return updatesSinceDeath;
}
public boolean isDead(){
return dead;
}
public void kill(){
dead = true;
updatesSinceDeath = 0;
try{
icon = ImageIO.read(new File("Explosion.png"));
}catch(Exception e){
e.printStackTrace();
}
}
public Point topLeft(){
return new Point(x, y);
}
public Point topRight(){
return new Point(x+icon.getWidth(), y);
}
public Point bottomLeft(){
return new Point(x, y+icon.getHeight());
}
public Point bottomRight(){
return new Point(x+icon.getWidth(), y+icon.getHeight());
}
public void setX(int x){
this.x = x;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
public void setY(int y){
this.y = y;
}
public double getVelX() {
return velX;
}
public void setVelX(double velX) {
this.velX = velX;
}
public double getVelY() {
return velY;
}
public void setVelY(double velY) {
this.velY = velY;
}
public BufferedImage getIcon() {
return icon;
}
public void setIcon(BufferedImage icon) {
this.icon = icon;
}
}

蓝色敌人.java

package com.darksunproductions.ld47.Entities;

import java.io.File;

import javax.imageio.ImageIO;

import com.darksunproductions.ld47.Game.Drawable;
import com.darksunproductions.ld47.Game.Game;

public class BlueEnemy extends Enemy implements Drawable{

public BlueEnemy(int x, int y, double velX, double velY, Game game){
super(x, y, velX, velY, game, null, Type.BLUE);

try{
setIcon(ImageIO.read(new File("Blue_Enemy.png")));
}catch(Exception e){
setIcon(null);
}
}

}

Controller .java

package com.darksunproductions.ld47.Game;

import java.awt.Graphics;
import java.awt.Point;
import java.util.LinkedList;

import com.darksunproductions.ld47.Entities.Enemy;
import com.darksunproductions.ld47.Entities.Entity;
import com.darksunproductions.ld47.Entities.Player;

public class Controller {

LinkedList<Drawable> entities = new LinkedList<Drawable>();
Game game;

public Controller(Game g){
game = g;
}

public void testCollision(int startID){
if(entities.size() > 0){
Entity e1 = (Entity) entities.get(startID);
Point corner[] = new Point[4];
corner[0] = e1.topLeft();
corner[1] = e1.topRight();
corner[2] = e1.bottomLeft();
corner[3] = e1.bottomRight();
for (int e = startID + 1; e < entities.size(); e++) {
Entity e2 = (Entity) entities.get(e);
for (int c = 0; c < 4; c++) {
if (corner[c].x > e2.topLeft().x && corner[c].x < e2.topRight().x) {
if (corner[c].y > e2.topLeft().y && corner[c].y < e2.bottomLeft().y) {
collide(e1, e2);
}
}
}
}
}
}

public void collide(Entity e1, Entity e2){
if(e1 instanceof Player && !e1.isDead()){
if(e2 instanceof Enemy && !e2.isDead()){
e1.kill();
e2.kill();
}
}
}

public void update(){
if(entities.size() > 0){
for(Drawable d : entities)
d.update();
}
}

public void render(Graphics g){
if(entities.size() > 0){
for(Drawable d : entities)
d.render(g);
}
}

public void add(Drawable d){
entities.add(d);
}

public void remove(Drawable d){
entities.remove(d);
}

public void remove(int i){
entities.remove(i);
}

public int size(){
return entities.size();
}
public int getIdOf(Entity e){
for(int i = 0; i < entities.size(); i++){
if(e == (Entity)entities.get(i))
return i;
}
return -1;
}
public int getIdOf(Drawable d){
Entity e = (Entity)d;
for(int i = 0; i < entities.size(); i++){
if(e == (Entity)entities.get(i))
return i;
}
return -1;
}

}

敌人.java

package com.darksunproductions.ld47.Entities;

import java.awt.Graphics;
import java.awt.image.BufferedImage;

import com.darksunproductions.ld47.Game.Drawable;
import com.darksunproductions.ld47.Game.Game;

public class Enemy extends Entity implements Drawable{

public enum Type{
BLUE
}

private Type type;

public Enemy(int x, int y, double velX, double velY, Game game, BufferedImage icon, Type type){
super(x, y, velX, velY, game, icon);
this.type = type;
}

public void render(Graphics g){
if(getIcon() != null)
g.drawImage(getIcon(), getX(), getY(), null);
}

public void update(){
if(getUpdatesSinceDeath() < 0){

setY(getY() + (int)getVelY());

if(getY() > 550){
setY(-50);
setX((int)(Math.random()*468));
}

}else if(getUpdatesSinceDeath() >= 0){
setUpdatesSinceDeath(getUpdatesSinceDeath() + 1);
if(getUpdatesSinceDeath() > 60){
game.c.remove(this);
}
}
}

}

这是他们碰撞时我遇到的问题:

Exception in thread "Thread-1" java.util.ConcurrentModificationException
at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:778)
at java.util.LinkedList$ListItr.next(LinkedList.java:713)
at com.darksunproductions.ld47.Game.Controller.update(Controller.java:52)
at com.darksunproductions.ld47.Game.Game.update(Game.java:58)
at com.darksunproductions.ld47.Game.Game.run(Game.java:78)
at java.lang.Thread.run(Thread.java:701)

除了一个小问题...当我点击 LinkedList.java:778 时,Eclipse 说 LinkedList 没有第 778 行...

预先感谢您提供的任何帮助...

最佳答案

game.c.remove(this); 是糟糕设计的一个很好的指标,实体没有责任决定何时删除它,只是向 提供信息>它可以使用的 Controller

主要问题是增强循环涉及 ListIterator,这会阻止底层 List 在您修改时被修改正在对其进行迭代。

相反,您自己使用 ListIterator 并且当 Controller 确定实体已失效(死亡或任何其他情况)时,使用 Iteratorremove 方法来移除它,例如

public void update(){
if(entities.size() > 0){
LinkedList<Drawable> entities = new LinkedList<>();
while (it.hasNext()) {
Drawable d = it.next();
d.update();
// Or what ever flag you want to use
if (d.isRemovable()) {
it.remove(d);
}
}
}
}

关于未从 LinkedList 中删除 Java 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31106303/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com