我發佈了這個Gamedev stackexchange,並建議在這裏發佈它。我有一個數組,基本上包含16 * 16塊。當我按WASD鍵,世界上移動時,所有的磚砸得到成海誓山盟,喜歡這裏的草磚做: IndexOutofbouds在遊戲的世界渲染
,並很快進入招我得到一個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;
}
}