gpt4 book ai didi

java - 将对象添加到 ArrayList - 获得 n 次最后找到的对象

转载 作者:行者123 更新时间:2023-11-29 09:01:22 25 4
gpt4 key购买 nike

我的项目有问题。当我从文件中读取我的游戏 map 时,我试图将 16 个对象(建筑物)添加到 ArrayList。毕竟,我得到 16 个相同的对象(最后在 map 上找到建筑物),我不知道为什么......

在游戏类中创建 ArrayList:

public static ArrayList<Building> buildingList = new ArrayList<>();

读取 map (Map类)【地形到Array,找到建筑到ArrayList】:

private void readBuilding(Terrain objct, int x, int y) {
System.out.format("Dodaję: ");
//Czerwone centrum dowodzenia
if(objct.getSSX() == 1 && objct.getSSY() == 3) {
Building b = new Building(x, y, 1, 1);
b.writeBuilding();
Game.buildingList.add(b);
}
//Czerwona miasto
if(objct.getSSX() == 2 && objct.getSSY() == 3) {
Building b = new Building(x, y, 2, 1);
b.writeBuilding();
Game.buildingList.add(b);
}
//Czerwona fabryka
if(objct.getSSX() == 3 && objct.getSSY() == 3) {
Building b = new Building(x, y, 3, 1);
b.writeBuilding();
Game.buildingList.add(b);
}
//Niebieskie centrum dowodzenia
if(objct.getSSX() == 1 && objct.getSSY() == 4) {
Building b = new Building(x, y, 1, 2);
b.writeBuilding();
Game.buildingList.add(b);
}
//Niebieska fabryka
if(objct.getSSX() == 2 && objct.getSSY() == 4) {
Building b = new Building(x, y, 2, 2);
b.writeBuilding();
Game.buildingList.add(b);
}
//Niebieskie miasto
if(objct.getSSX() == 3 && objct.getSSY() == 4) {
Building b = new Building(x, y, 3, 2);
b.writeBuilding();
Game.buildingList.add(b);
}
//Niczyje miasto
if(objct.getSSX() == 4 && objct.getSSY() == 3) {
Building b = new Building(x, y, 3, 0);
b.writeBuilding();
Game.buildingList.add(b);
}
//Niczyja fabryka
if(objct.getSSX() == 4 && objct.getSSY() == 4) {
Building b = new Building(x, y, 2, 0);
b.writeBuilding();
Game.buildingList.add(b);
}
}


private void createMap() {
map = new Terrain[40][22];
String[] fields = csvFileContent.split(";");
int x, y;
int fieldNo = 0;

for(y = 0; y < 22; y++) {
for(x = 0; x < 40; x++) {
Terrain objct = new Terrain();
objct.setSSX(Integer.parseInt(fields[fieldNo].substring(0,2)));
objct.setSSY(Integer.parseInt(fields[fieldNo].substring(2,4)));
if(objct.getSSY() > 2) {
readBuilding(objct, x, y);
objct.setSSX(3);
objct.setSSY(1);
}
fieldNo++;
map[x][y] = objct;
}
}
System.out.format("Elementow: %d\n",Game.buildingList.size());
for(Building b : Game.buildingList) {
b.writeBuilding();
}
}

在屏幕上渲染 map (类 map ):

public void render(Graphics graphics, Game game) {
int x, y;
//System.out.println("Budynkow: " + game.getBuildingList().size());
for(y = 0; y < 22; y++) {
for(x = 0; x < 40; x++) {
field = ss.grabImage(map[x][y].getSSX(), map[x][y].getSSY(), 32, 32);
graphics.drawImage(field, x*32, y*32, null);
}
}
Iterator<Building> it = Game.buildingList.iterator();
while(it.hasNext()) {
Building b = it.next();
field = ss.grabImage(b.getSSX(), b.getSSY(), 32, 32);
graphics.drawImage(field, b.getX()*32, b.getY()*32, null);
}
}

我也试过像这样迭代 List:

for(Building b : Game.buildingList) {
field = ss.grabImage(b.getSSX(), b.getSSY(), 32, 32);
graphics.drawImage(field, b.getX()*32, b.getY()*32, null);
}

而且它也不起作用。

Map类中的方法:

b.writeBuilding();

当我将它写入 NetBeans 控制台时,它为我提供了正确的字段值,但它一直在添加相同的值。

类地形:

package game;

class Terrain {
private int ssX;
private int ssY;
private int x;
private int y;
private boolean occupied;
private boolean crossable;

Terrain() {
this.occupied = false;
}

public void setSSX(int ssX) {
this.ssX = ssX;
}

public void setSSY(int ssY) {
this.ssY = ssY;
}

public void setX(int x) {
this.x = x;
}

public void setY(int y) {
this.y = y;
}

public int getSSX() {
return ssX;
}

public int getSSY() {
return ssY;
}

public int getX() {
return x;
}

public int getY() {
return y;
}

public void setCrossable() {
if(ssX == 2 && ssY == 2) {
this.crossable = false;
} else {
if(ssX >= 6 && ssX <= 11) {
if(ssY >= 1 && ssY <= 2) {
this.crossable = false;
} else {
this.crossable = true;
}
}
}
}
}

类(class) build :

package game;

public class Building {
private static int owner;
private static int type;
private static int x;
private static int y;
private static int ssX;
private static int ssY;

public Building(int x, int y, int type, int owner) {
this.owner = owner;
this.type = type;
this.x = x;
this.y = y;

if(this.owner == 0) {
if(this.type == 2) {
this.ssX = 4;
this.ssY = 4;
} else if(this.type == 3) {
this.ssX = 4;
this.ssY = 3;
}
} else if(this.owner == 1) {
if(this.type == 1) {
this.ssX = 1;
this.ssY = 3;
} else if(this.type == 2) {
this.ssX = 2;
this.ssY = 3;
} else if(this.type == 3) {
this.ssX = 3;
this.ssY = 3;
}
} else if(this.owner == 2) {
if(this.type == 1) {
this.ssX = 1;
this.ssY = 4;
} else if(this.type == 2) {
this.ssX = 2;
this.ssY = 4;
} else if(this.type == 3) {
this.ssX = 3;
this.ssY = 4;
}
}
}

public int getX() {
return x;
}
public int getY() {
return y;
}
public int getSSX() {
return ssX;
}
public int getSSY() {
return ssY;
}

public void writeBuilding() {
System.out.format("Owner: %d Type: %d X: %d Y: %d SSX: %d SSY %d\n"
,this.owner,this.type,this.x,this.y,this.ssX,this.ssY);
}
}

有什么想法吗?在此先感谢您的帮助。 ;)

最佳答案

构建使用静态字段,每次初始化对象时都会覆盖这些字段,因此最终所有字段都是相等的。

关于java - 将对象添加到 ArrayList - 获得 n 次最后找到的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17249187/

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