0

Im trying to set the x and y of these rectangles but any time i try they keep setting at 99. No matter where i set it it keeps going to that. As far as i know the only thing i can think of is that the Block classes x and y made inside the classare set to 99 and when i sat a block in the array to that it changes to 99. but im setting bounds after it sets the block so thats why im confused.

Setting blocks:

public class World {

public static Block[][] block = new Block[100][100];
public static Character character = new Character(40, 20, "Player001");
public static Point mse = new Point(0, 0);

public static void generateWorld() {
    for (int x = 0; x < block.length; x++) {
        for (int y = 0; y < block[0].length; y++) {
            block[x][y] = Block.air;
            if(y > 10) {
                if(new Random().nextInt(5) == 1){
                    block[x][y] = Block.dirt;
                }
            }
            block[x][y].setBounds(new Rectangle(x * Block.size, y * Block.size, Block.size, Block.size));
        }
    }
}

public static void tick() {
    System.out.println(block[5][5].x);
    character.tick();
    for (int x = 0; x < block.length; x++) {
        for (int y = 0; y < block[0].length; y++) {
            block[x][y].tick();
        }
    }
}

public static void render(Graphics g) {
    for (int x = 0; x < block.length; x++) {
        for (int y = 0; y < block[0].length; y++) {
            g.drawImage(block[x][y].image, x * Block.size
                    - Character.camX,
                    y * Block.size + Character.camY,
                    Block.size, Block.size, null);
        }
    }
    character.render(g);
}

}

Block class:

public class Block extends Rectangle {

private static final long serialVersionUID = 6859844338993954281L;

public static int size = 16;
public String name;
public int id;
public Image image;
public static int blockCount = 0;
public boolean isSolid;

public Block(String name, int id, boolean isSolid) {
    this.name = name;
    this.id = id;
    this.isSolid = isSolid;
    try {
        image = ImageIO.read(getClass().getResourceAsStream(
                "/Blocks/" + name + ".png"));
    } catch (Exception e) {
        e.printStackTrace();
    }
    blockCount++;
}

public void tick() {

}

public static Block air = new Block("air", 0, false);
public static Block dirt = new Block("dirt", 1, true);

}
4

2 回答 2

0

Block.air is just one object. You are setting most of your block array to contain references to that single object, which causes all of those elements to share that Block object:

Array elements pointing to Block.air

As you might expect, then, calling setBounds immediately alters the original Block.air, and most of the elements see that change (since most of the elements point to the same shared Block.air object).

What you need to do is make each element of your block array its own object. The clone() method inherited from Rectangle is probably the easiest way:

Random random = new Random();
block[x][y] = (Block) Block.air.clone();
if (y > 10) {
    if (random.nextInt(5) == 1) {
        block[x][y] = (Block) Block.dirt.clone();
    }
}
block[x][y].setBounds(x * Block.size, y * Block.size, Block.size, Block.size);
于 2014-12-18T03:26:01.427 回答
0

你怎么知道你的 x 和 y 值是 99?是因为 render 方法把所有东西都放在了同一个地方吗?你能在 IDE 中打开 block[][] 数组吗?如果没有,请考虑在将每个块放入数组时打印出它的内容。

考虑像这样重写你的循环。

public static void generateWorld() {
    Random random = new Random();
    Block result;

    for (int x = 0; x < block.length; x++) {
        for (int y = 0; y < block[0].length; y++) {
            if((y > 10) && ((random.nextInt(5) == 1)){
                result = Block.dirt;
            }
            else {
                result = Block.air;  
            }
            result.setBounds(x * Block.size, y * Block.size, Block.size, Block.size);
            block[x][y] = result;
            System.out.println ("block at position (" + x + "," + " + y + ") = " + result.x, result.y, result.width, result.height);
        }
    }
}

所以我改变了一些东西。我不会每次都重新实例化 Random,一个实例就足够了。我使用一个“持有人”变量来保存进入数组的值,这样我就可以在不使用 [x][y] 符号的情况下修改它。该表示法没有任何问题,但在 println() 语句中会很乏味。最后,我确保依次打印出每个块的内容。这应该向您表明您的值确实被正确初始化。

运行此程序时,您可能会考虑将 block[][] 数组的大小减小到 10x10。直到你弄清楚发生了什么。

如果块在初始化时都正确打印出来,那么它会告诉您有问题,然后它会告诉您显示数组内容的代码部分有问题。(你没有包括在内)。

我希望这有帮助。

于 2014-12-18T03:12:16.713 回答