我的项目有问题。当我从文件中读取我的游戏的地图时,我正在尝试将 16 个对象(建筑物)添加到 ArrayList。毕竟,我得到了 16 个相同的对象(最后在地图上找到的建筑物),我不知道为什么......
在 Game 类中创建 ArrayList:
public static ArrayList<Building> buildingList = new ArrayList<>();
读取地图(地图类)[Terrain 进入 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();
}
}
在屏幕上渲染地图(类地图):
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;
}
}
}
}
}
班级建设:
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);
}
}
有任何想法吗?提前感谢您的帮助。;)