2014-01-10 64 views
0

我发布了这个Gamedev stackexchange,并建议在这里发布它。我有一个数组,基本上包含16 * 16块。当我按WASD键,世界上移动时,所有的砖砸得到成海誓山盟,喜欢这里的草砖做: http://puu.sh/6g0hp.pngIndexOutofbouds在游戏的世界渲染

,并很快进入招我得到一个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

在这里你可以定义你的世界:

public Block[][] worldContents=new Block[400][400]; 

为400x400的二维数组。数组的定义要求记下数组的确切大小,在这种情况下(400和400),但是当访问数组索引时,从0开始,这意味着在得到IndexOutOfBoundsException之前,您只能计数到399。

所以更改

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; 

希望这有助于

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