我正在使用libGdx关注视频教程,并调整代码以获得我想要的内容。我设法绘制了HUD和一个动画,但是当我尝试在另一个教程之后添加一个按钮时,我以黑色屏幕结束,这里是代码:
public class WorldRenderer implements ApplicationListener, Screen {
Pixmap pixmap;
SpriteBatch batch;
private Skin skin;
private Stage stage;
OrthographicCamera cam;
Table table;
TextButton buttonShake;
BitmapFont white;
BitmapFont black;
private TextureAtlas atlas;
Texture virusTexture;
Texture hudTexture;
private static final float HUD_WIDTH = 480f;
private static final float HUD_HEIGHT = 800f;
private static final float WALK_ANIM_WIDTH = 337f;
private static final float WALK_ANIM_HEIGHT = 213f;
public static final int SCREEN_WIDTH = 480;
public static final int SCREEN_HEIGHT = 800;
private static final int FRAME_COLS = 2;
private static final int FRAME_ROWS = 2;
Animation walkAnimation;
Texture walkSheet;
TextureRegion[] walkFrames;
TextureRegion currentFrame;
float stateTime;
float width, height;
Hud hud;
public WorldRenderer(World world) {
this.world = world;
walkSheet = new Texture("data/character.png");
TextureRegion[][] tmp = TextureRegion.split(walkSheet,
walkSheet.getWidth() / FRAME_COLS, walkSheet.getHeight()
/ FRAME_ROWS);
walkFrames = new TextureRegion[FRAME_COLS * FRAME_ROWS];
int index = 0;
for (int i = 0; i < FRAME_ROWS; i++) {
for (int j = 0; j < FRAME_COLS; j++) {
walkFrames[index++] = tmp[i][j];
}
}
walkAnimation = new Animation(0.25f, walkFrames);
stateTime = 0f;
width = Gdx.graphics.getWidth();
height = Gdx.graphics.getHeight();
cam = new OrthographicCamera(SCREEN_WIDTH, SCREEN_HEIGHT);
cam.position.set(SCREEN_WIDTH / 2f, SCREEN_HEIGHT / 2f, 0);
batch = new SpriteBatch();
batch.setProjectionMatrix(cam.combined);
hudTexture = new Texture("data/hud.png");
hudTexture.setFilter(TextureFilter.Linear, TextureFilter.Linear);
}
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
cam.update();
stage.act(delta);
stateTime += Gdx.graphics.getDeltaTime();
currentFrame = walkAnimation.getKeyFrame(stateTime, true);
hud = world.getHud();
batch.setProjectionMatrix(cam.combined);
batch.begin();
batch.draw(hudTexture, SCREEN_WIDTH / 2f - HUD_WIDTH / 2f,
SCREEN_HEIGHT / 2f - HUD_HEIGHT / 2f);
batch.draw(currentFrame, SCREEN_WIDTH / 2f - WALK_ANIM_WIDTH / 2f,
SCREEN_HEIGHT / 2f - WALK_ANIM_HEIGHT / 2f);
batch.end();
stage.draw();
}
public void dispose() {
batch.dispose();
virusTexture.dispose();
hudTexture.dispose();
skin.dispose();
atlas.dispose();
white.dispose();
black.dispose();
stage.dispose();
}
@Override
public void create() {
}
@Override
public void resize(int width, int height) {
}
@Override
public void pause() {
// TODO Auto-generated method stub
}
@Override
public void resume() {
// TODO Auto-generated method stub
}
@Override
public void show() {
}
@Override
public void hide() {
// TODO Auto-generated method stub
}
@Override
public void render() {
// TODO Auto-generated method stub
}
我很确定你也会找到未使用过的代码行,所以如果你愿意的话请指出它,我可以更好地组织代码并学习。谢谢!
答案 0 :(得分:2)
一个可能的问题是你实现了一个ApplicationListener和一个Screen,它们都有render方法和几乎相同的方法。
你不应该同时实现这两个。如果这是您的应用入口点,我会选择applicationListener。
但是,出于教育目的,您应该阅读有关Game类的一些主题作为您的下一个应用入口点。考虑使用处理渲染部分的分离“屏幕”和允许它们相互通信的“大脑”,这就是Game类变得方便的地方。
开头的事情看起来有点棘手但经过一些学习后肯定会对你有意义;)