有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

游戏世界渲染中的java IndexOutofbouds

我在Gamedev stackexchange上发布了这篇文章,有人建议我将其发布在这里。我有一个数组,基本上包含16*16个块。当我按下wasd键时,整个世界都在移动,所有的瓷砖都会相互碰撞,就像这里的草瓷砖一样: http://puu.sh/6g0hp.png

搬家后不久,我得到了一个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;
    }

}

共 (2) 个答案

  1. # 1 楼答案

    你在这里定义你的世界:

    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; 
    

    希望这有帮助

  2. # 2 楼答案

    您可能正在访问数组的索引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