Libgdx游戏

由于某种原因,我的SpriteBatch无法渲染我用来构建地牢的两个2d int数组(在Dungeon1类中)。

我创建了一个随机地牢构建算法,该算法输入到我的2d整数数组中。我正在尝试创建一个单独的具有自己的纹理的Dungeon类,该类实现第一类的dungeon-building算法,但由于某些原因,地图将无法渲染。

在主游戏类中,设置屏幕:

public class NanoRealms extends Game {
    @Override
    public void create() {
        this.setScreen(new Dungeon1(this));
    }
}


然后,我使用此类来构建地牢:

public class Dungeon {

public static final int tileSIZE = 64;

private Random rand = new Random();

static int mapSize = 128;
static int[][] bg = new int[mapSize][mapSize];
static int[][] fg = new int[mapSize][mapSize];

private int roomCount = rand.nextInt(20) + 10;
private int minSize = 10;
private int maxSize = 20;

private ArrayList<Room> rooms = new ArrayList<Room>();
private Room[] room = new Room[roomCount];

public Dungeon() {
    makeRooms();
    squashRooms();
    makeCorridors();
    fillRoom();
    Autotile at = new Autotile(mapSize, bg, fg);
}
...Dungeon Building Algorithm Goes HERE...
}


但是,当我尝试渲染输入到2个2d数组中的输入时,它将无法正常工作。

您可以在我的此类的render方法中看到以下内容:

public class Dungeon1 implements Screen {

public Texture tiles;
public TextureRegion floor, wall, wallLeft, wallRight, tlCorn, trCorn, blCorn, brCorn, c1, c2, c3, c4;

float wh = Gdx.graphics.getWidth();
float ht = Gdx.graphics.getHeight();

private SpriteBatch batch;
private OrthographicCamera camera;

private int[][] fg = Dungeon.fg;
private int[][] bg = Dungeon.bg;

public Dungeon1(NanoRealms game) {
    Dungeon d = new Dungeon();
    tiles = new Texture(Gdx.files.internal("Map/tiles.png"));
    floor = new TextureRegion(tiles, 2 * 64, 0, Dungeon.tileSIZE, Dungeon.tileSIZE);
    wall = new TextureRegion(tiles, 64, 64, Dungeon.tileSIZE, Dungeon.tileSIZE);
    wallLeft = new TextureRegion(tiles, 0, 2 * 64, Dungeon.tileSIZE, Dungeon.tileSIZE);
    wallRight = new TextureRegion(tiles, 2 * 64, 2 * 64, Dungeon.tileSIZE, Dungeon.tileSIZE);
    tlCorn = new TextureRegion(tiles, 0, 64, Dungeon.tileSIZE, Dungeon.tileSIZE);
    trCorn = new TextureRegion(tiles, 2 * 64, 64, Dungeon.tileSIZE, Dungeon.tileSIZE);
    blCorn = new TextureRegion(tiles, 0, 3 * 64, Dungeon.tileSIZE, Dungeon.tileSIZE);
    brCorn = new TextureRegion(tiles, 2 * 64, 3 * 64, Dungeon.tileSIZE, Dungeon.tileSIZE);
    c1 = new TextureRegion(tiles, 3 * 64, 64, Dungeon.tileSIZE, Dungeon.tileSIZE);
    c2 = new TextureRegion(tiles, 4 * 64, 64, Dungeon.tileSIZE, Dungeon.tileSIZE);
    c3 = new TextureRegion(tiles, 3 * 64, 2 * 64, Dungeon.tileSIZE, Dungeon.tileSIZE);
    c4 = new TextureRegion(tiles, 4 * 64, 2 * 64, Dungeon.tileSIZE, Dungeon.tileSIZE);

    batch = new SpriteBatch();
    camera = new OrthographicCamera(30, 30 * (Gdx.graphics.getHeight()/Gdx.graphics.getWidth()));
    camera.position.set(3000, 3000, 0);
    camera.zoom = 300;
}

public void render(float delta) {
    cameraInput();


    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL30.GL_COLOR_BUFFER_BIT);

    ////
    batch.begin();
    batch.setProjectionMatrix(camera.combined);
    batch.enableBlending();
    for (int x = 0; x < bg.length; x++) {
        float w = x * 64;
        for (int y = 0; y < bg[x].length; y++) {
            float h = y * 64;
            if (bg[x][y] == 1) {
                batch.draw(floor, w, h);
            }
        }
    }
    for (int x2 = 0; x2 < fg.length; x2++) {
        float w2 = x2 * 64;
        for (int y2 = 0; y2 < fg[x2].length; y2++) {
            float h2 = y2 * 64;
            if (fg[x2][y2] == 2) {
                batch.draw(wall, w2, h2);
            } else if (fg[x2][y2] == 3) {
                batch.draw(wallLeft, w2, h2);
            } else if (fg[x2][y2] == 4) {
                batch.draw(wallRight, w2, h2);
            } else if (fg[x2][y2] == 5) {
                batch.draw(tlCorn, w2, h2);
            } else if (fg[x2][y2] == 6) {
                batch.draw(trCorn, w2, h2);
            } else if (fg[x2][y2] == 7) {
                batch.draw(blCorn, w2, h2);
            } else if (fg[x2][y2] == 8) {
                batch.draw(brCorn, w2, h2);
            } else if (fg[x2][y2] == 9) {
                batch.draw(c1, w2, h2);
            } else if (fg[x2][y2] == 10) {
                batch.draw(c2, w2, h2);
            } else if (fg[x2][y2] == 11) {
                batch.draw(c3, w2, h2);
            } else if (fg[x2][y2] == 12) {
                batch.draw(c4, w2, h2);
            }
        }
    }
    batch.end();
    ////
}


public void cameraInput() {
    if (Gdx.input.isKeyPressed(Keys.Q))
        camera.zoom += 0.5;
    if (Gdx.input.isKeyPressed(Keys.E))
        camera.zoom -= 0.5;
    if (Gdx.input.isKeyPressed(Keys.A))
        camera.translate(-10, 0, 0);
    if (Gdx.input.isKeyPressed(Keys.D))
        camera.translate(10, 0, 0);
    if (Gdx.input.isKeyPressed(Keys.S))
        camera.translate(0, -10, 0);
    if (Gdx.input.isKeyPressed(Keys.W))
        camera.translate(0, 10, 0);
}
}


当我将Dungeon1的render方法和Textures放入Dungeon时,一切正常。但是,我想创建具有不同纹理的不同样式的地牢,而不必每次都重写我的地牢算法。

最佳答案

您没有说出实际出了什么问题,所以我只是在猜测。

跳到我身上的问题是Dungeon1扩展了Dungeon,但没有调用超级构造函数,因此永远不会调用方法makeRooms()squashRooms()等。您确实创建了一个Dungeon对象​​,然后对其不执行任何操作,因此将其丢弃。

因此,在Dungeon1构造函数的顶部,替换

Dungeon d = new Dungeon();




super();


附带说明一下,在Dungeon中有静态数组bgfg似乎很奇怪,然后您的子类用名称相同的数组隐藏了它们,然后将这些非静态数组指向地牢中的静态数组。这令人费解,还有一个错误等待发生。这些看起来不应该是静态的,因此我认为您可以从它们两个中都删除static关键字。子类根本不需要引用它们。这是一个子类,因此它可以正常访问它们而无需“重新引用”它们。

07-28 03:07