【问题标题】:Unable to Render 2D Array from Other Class无法从其他类渲染二维数组
【发布时间】:2015-09-28 22:06:08
【问题描述】:

Libgdx 游戏

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

我创建了一个随机地牢构建算法,该算法将输入到我的二维整数数组中。我正在尝试使用自己的纹理创建一个单独的 Dungeon 类,该类实现了第一类中的地牢构建算法,但由于某种原因,地图不会渲染。

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

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 个二维数组中的输入时,它不起作用。

您可以在我的此类的渲染方法中看到这一点:

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 中的渲染方法和纹理放入 Dungeon 时,一切正常。但是,我想创建具有不同纹理的不同风格的地下城,而不必每次都重写我的地下城算法。

【问题讨论】:

  • 定义“行不通”

标签: java libgdx 2d


【解决方案1】:

你没有说到底出了什么问题,所以我只是猜测。

让我跳出来的问题是 Dungeon1 扩展了 Dungeon,但没有调用超级构造函数,所以你的方法 makeRooms()squashRooms() 等永远不会被调用。你确实创建了一个 Dungeon 对象,然后什么都不做,所以它被扔掉了。

所以在 Dungeon1 构造函数的顶部,替换

Dungeon d = new Dungeon();

super();

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

【讨论】:

  • Dungeon1 类实际上并不是 Dungeon 的子类,只是实现了屏幕接口。
  • 我没有注意到这一点。我想我明白了... Dungeon1 只是一个渲染器,就像地图的皮肤一样?就像我说的,这只是一个猜测。你没有说什么实际上不起作用。
  • 是的,修复了它,你说的所有错误我都调整了。现在dungeon1 扩展了dungeon,并且我在dungeon() 中添加了一个构造函数。非常感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-09-28
  • 2021-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-14
  • 1970-01-01
相关资源
最近更新 更多