我在Gamedev stackexchange上发布了这个,并建议在此发布。我有一个数组,基本上包含16 * 16块。当我按下wasd键时,世界会移动,所有的瓷砖都会被打碎,就像这里的草砖一样:
并且在移动过程中我得到了一个IndexOutOfBounds异常。 400或-1。我应该做什么而不是移动renderX和renderY,以使其工作? 我的最新尝试:
package net.makerimages.sandbox.world;
import net.makerimages.sandbox.block.*;
import net.makerimages.sandbox.world.biome.Biomes;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import java.util.Random;
/**
* Created by Makerimages on 5.01.14.
*/
public class World
{
private Biomes currentGeneratorBiome;
private Random rand=new Random();
private int worldSeed;
public Block[][] worldContents=new Block[400][400];
public BlockDirt blockDirt=new BlockDirt(0,this);
public BlockAir blockAir=new BlockAir(1,this);
public BlockStone blockStone=new BlockStone(2,this);
public BlockGrass blockGrass=new BlockGrass(3,this);
public BlockCobbleStone blockCobbleStone=new BlockCobbleStone(4,this);
public BlockSand blockSand=new BlockSand(5,this);
public int renderX=-1;
public int renderY=-1;
public World() throws SlickException {
}
public void renderWorld(GameContainer gameContainer)
{
int xmin= renderX/16;
int xmax= renderX+800/16;
int ymin= renderY/16;
int ymax=renderY+600/16;
for(int x=xmin;x< xmax;x++)
{
for(int y=ymin;y<ymax;y++)
{
if (x < 0) x = 0;
if (y < 0) y = 0;
if (x >= 400) x = 400; // width of map
if (y >= 400) y = 400; // height of map
worldContents[x][y].draw(x*16-renderX,y*16-renderY);
}
} Input I=gameContainer.getInput();
if(I.isKeyDown(Input.KEY_S))
{
renderY++;
}
if(I.isKeyDown(Input.KEY_W))
{
renderY--;
}
if(I.isKeyDown(Input.KEY_A))
{
renderX--;
}
if(I.isKeyDown(Input.KEY_D))
{
renderX++;
}
}
public void generateNewWorld()
{
int startY=7;
int placementY;
int currentY=startY;
Random rand=new Random();
for(int x=0; x<worldContents.length;x++)
{
//Plainlands biome
int upDown=rand.nextInt(45);
if(upDown>22)
{
placementY=currentY+rand.nextInt(2);
}
else
{
placementY=currentY-rand.nextInt(2);
if(placementY<0)
{
placementY=-placementY;
}
}
worldContents[x][placementY]=blockDirt;
for(int y=placementY+1;y<worldContents[0].length-(placementY+1);y++)
{
worldContents[x][y]=blockStone;
}
currentY=placementY;
}
for(int xP=0;xP<worldContents.length;xP++)
{
for(int yP=0;yP<worldContents[0].length;yP++)
if(worldContents[xP][yP]==null)
{
worldContents[xP][yP]=blockAir;
}
}
}
public Block getBlockAt(int x, int y)
{
return worldContents[x][y];
}
public void setBlockAt(int x, int y, Block block)
{
worldContents[x][y]=block;
}
}
答案 0 :(得分:0)
您可能正在访问数组的索引400。在这里
if (x >= 400) x = 400; // width of map
if (y >= 400) y = 400; // height of map``
worldContents[x][y].draw(x*16-renderX,y*16-renderY);
您的worldContents
是400x400矩阵,这意味着索引从0到399。
答案 1 :(得分:0)
你在这里定义你的世界:
public Block[][] worldContents=new Block[400][400];
作为400x400 2D阵列。数组的定义要求你注意你想要的数组的确切大小,在这种情况下(400和400),但是当访问数组时索引从0开始意味着在得到IndexOutOfBoundsException之前你只能计数到399。 / p>
所以改变
if (x >= 400) x = 400;
if (y >= 400) y = 400;
到
if (x >= 399) x = 399;
if (y >= 399) y = 399;
或
if (x >= worldContents[0].length) x = worldContents[0].length;
if (y >= worldContents.length) y = worldContents.length;
希望这有帮助