2

我正在关注使用 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

}

我很确定您还会找到未使用的代码行,因此如果您愿意,请为我指出,以便我更好地组织代码并学习。谢谢!

4

1 回答 1

2

一个可能的问题是您实现了一个 ApplicationListener 和一个 Screen,它们都具有 render 方法和几乎相同的方法。

你不应该同时实现两者。如果这是您的应用程序入口点,我会选择 applicationListener。

但是,出于教育目的,您应该阅读一些有关 Game 类的主题作为您的下一个应用程序入口点。考虑使用处理渲染部分的分离“屏幕”和允许它们相互通信的“大脑”,这就是 Game 类变得方便的地方。

事情一开始看起来有点棘手,但经过一些学习后肯定会对你有意义;)

于 2013-08-12T01:57:09.853 回答