0

我正在尝试仅使用精灵创建一个主菜单。但是,当它被渲染时,精灵会快速闪烁。为什么要这样做?

@Override
public void show() {
    batch = new SpriteBatch();

    texture = new Texture("ui/mainmenu.png");
    mainMenu = new Sprite(texture);
    mainMenu.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

    batch.begin();
    mainMenu.draw(batch);
    batch.end();
}

@Override
public void render(float delta) {
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
}
4

3 回答 3

2

这在您的渲染方法中:

batch.begin();
mainMenu.draw(batch);
batch.end();

Render 是不断调用的方法,show 只在屏幕显示时调用一次。

于 2013-08-19T17:36:29.277 回答
0

如果你使用这个确切的代码,事情应该会奏效。每一帧,你清除你的屏幕,然后渲染你的精灵。

@Override
public void show() {
    batch = new SpriteBatch();

    texture = new Texture("ui/mainmenu.png");
    mainMenu = new Sprite(texture);
    mainMenu.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

}

@Override
public void render(float delta) {
    //clear screen to black.
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    //render sprite
    batch.begin();
    mainMenu.draw(batch);
    batch.end();
}
于 2013-08-19T20:46:37.823 回答
0

使用正交相机渲染您的精灵。这可能对你有用

OrthographicCamera cam;
@Override
public void show() {
    batch = new SpriteBatch();
    cam= new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
    cam.position.set(Gdx.graphics.getWidth()/2, Gdx.graphics.getHeight()/2,0);
    cam.update();
    texture = new Texture("ui/mainmenu.png");
    mainMenu = new Sprite(texture);
    mainMenu.setSize(Gdx.graphics.getWidth(), Gdx.graphics.getHeight());

}

@Override
public void render(float delta) {
    //clear screen to black.
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    //render sprite
    batch.setProjectionMatrix(cam.combined);
    batch.begin();
    mainMenu.draw(batch);
    batch.end();
}
于 2013-08-20T07:55:29.683 回答