我正在使用 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
}
我很确定您也会找到未使用的代码行,因此如果您愿意,请为我指出它们,以便我可以更好地组织我的代码并学习。谢谢!
最佳答案
一个可能的问题是您实现了一个 ApplicationListener 和一个 Screen,两者都有 render 方法并且几乎相同的方法。
您不应该同时实现两者。如果这是您的应用程序入口点,我会选择 applicationListener。
但是,出于教育目的,您应该阅读一些有关 Game 类的主题作为您的下一个应用程序入口点。考虑使用单独的“屏幕”来处理渲染部分和一个“大脑”来允许它们相互通信,这就是 Game 类变得方便的地方。
一开始事情可能看起来有点棘手,但经过一番研究后肯定会对你有意义;)
关于java - 黑屏和清理代码(LibGDX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18114924/