【发布时间】: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 时,一切正常。但是,我想创建具有不同纹理的不同风格的地下城,而不必每次都重写我的地下城算法。
【问题讨论】:
-
定义“行不通”