2013-10-01 51 views
0

由於某些原因,塊保持呈現在相同的位置。有誰能夠幫助我?在Java中使用2d地形生成器時出現問題

Block[][] chunk = new Block[Chunk.CHUNK_WIDTH_BLOCKS][Chunk.CHUNK_HEIGHT_BLOCKS]; 
    float[][] positions = new float[Chunk.CHUNK_WIDTH_BLOCKS][Chunk.CHUNK_HEIGHT_BLOCKS]; 
    float frequency = 1.0f/(float) chunk.length; 

    for (int x = 0; x < chunk.length - 1; x++) 
    { 
     for (int y = 0; y < chunk[x].length - 1; y++) 
     { 
      positions[x][y] = SimplexNoise.Generate((float) x * frequency, (float) y * frequency); 
      g.drawRect(positions[x][0], positions[0][y], Block.BLOCK_WIDTH, Block.BLOCK_HEIGHT); 
     } 
    } 

    for (int x = 0; x < Chunk.CHUNK_WIDTH_BLOCKS; x++) 
    { 
     for (int y = 0; y < Chunk.CHUNK_HEIGHT_BLOCKS; y++) 
     { 
      if (positions[x][y] < 0f) 
       chunk[x][y] = new Block(); 
      if (positions[x][y] >= -0f) 
       chunk[x][y] = new Block(); 
     } 
} 

回答

1

您的代碼有多個問題。例如:

for (int x = 0; x < chunk.length - 1; x++) 

這應該是:

for (int x = 0; x < chunk.length; x++) 

此外,考慮以下因素:

g.drawRect(positions[x][0], positions[0][y], Block.BLOCK_WIDTH, Block.BLOCK_HEIGHT); 

在 「位置[X] [Y]」 這不會使用所有值....我認爲你想要的是數組爲3d ...例如:

float[][][] positions = new float[Chunk.CHUNK_WIDTH_BLOCKS][Chunk.CHUNK_HEIGHT_BLOCKS][2]; 

這樣:posistions [X] [Y] [0]是x的值,並且位置[X] [Y] [1]是y的值....

g.drawRect(positions[x][y][0], positions[x][y][1], Block.BLOCK_WIDTH, Block.BLOCK_HEIGHT); 

我不知道我完全理解你的代碼,但它確實有問題。

相關問題