我目前正在为我的 libgdx 游戏制作一个简单的加载屏幕,但是它有一个问题。加载屏幕在 Android 项目上完美运行,但在桌面版本上则无法运行。当前加载屏幕应显示“正在加载”->“正在加载”。 -> "LOADING.."-> "LOADING...",间隔 1 秒然后重复。当我在桌面应用程序上加载游戏时,它显示“正在加载”(并且它疯狂地闪烁),然后按照预期的方式继续进行。整个过程中,一切都在闪烁,就像刷新率太高之类的,并且它在背景中显示了不应该存在的幻影周期。有人可以告诉我桌面版本何时发生这种情况,而不是 Android 版本吗?这也是我的“LoadingScreen Implements Screen”的两个函数的代码
渲染(float delta)函数:
float updateTextTime = 0;
@Override
public void render(float delta) {
if (updateTextTime >= 1){ //check to see if it has been a second
if (periods == 0){
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT); //clears the buffer bit
}
textBatch.begin();
font.draw(textBatch,loading.substring(0,7+periods),(Gdx.graphics.getWidth()/2)-(textwidth/2),(Gdx.graphics.getHeight()/2)+(textheight/2));
if (periods < 3){
periods += 1;
}else{
periods = 0;
}
textBatch.end();
updateTextTime = 0;
}else{
updateTextTime += delta; //accumlate
}
}
和 show() 函数:
@Override
public void show() {
textBatch = new SpriteBatch();
font = new BitmapFont(Gdx.files.internal("fonts/ArmyChalk.fnt"), Gdx.files.internal("fonts/ArmyChalk.png"),false);
if (game.AppTypeMine == ApplicationType.Android){
font.scale(2.0f);
}
textwidth = font.getBounds(loading).width;
textheight = font.getBounds(loading).height;
}
最终解决方案: 更改了 render() 函数: 公共(public)无效渲染(浮点增量){
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
textBatch.begin();
font.draw(textBatch,loading.substring(0,7+periods),(Gdx.graphics.getWidth()/2)-(textwidth/2),(Gdx.graphics.getHeight()/2)+(textheight/2));
textBatch.end();
if (updateTextTime >= 0.5){ //check to see if it has been a second
if (periods < 3){
periods += 1;
}else{
periods = 0;
}
updateTextTime = 0;
}else{
updateTextTime += delta; //accumlate
}
}
最佳答案
您应该始终在 render
回调中渲染某些内容。
您可能会遇到双缓冲问题(OpenGL 无法判断您没有在当前帧缓冲区中绘制任何内容,因此它最终会渲染其中剩余的内容)。
更改您的代码,以便始终调用 font.draw
调用。我认为您可以将 textBatch.begin()
、font.draw()
和 textBatch.end()
调用移到初始位置之外if/else block 。
如果你不希望渲染被如此频繁地调用,你可以将 Libgdx 切换到非连续渲染模式(但你需要找到一个钩子(Hook)来让渲染调用每秒左右恢复一次) )。请参阅http://bitiotic.com/blog/2012/10/01/enabling-non-continuous-rendering-in-libgdx/ .
关于java - libgdx 桌面应用程序屏幕闪烁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17507180/