我正在开发一个关于Libgdx的游戏。我游戏的背景只是Gl.clear颜色。如果分数大于5,我想要平滑地改变背景。那么我怎么能用Gl.clearColor来做。或者我需要尝试其他的东西?
答案 0 :(得分:1)
您可以查看ColorAction获取灵感,也可以直接使用它:
Color color = new Color(Color.WHITE);
ColorAction colorAction = new ColorAction();
public MyGame() {
colorAction.setColor(color);
colorAction.setDuration(2);
colorAction.setEndColor(Color.RED);
}
public void render(float delta) {
Gdx.gl.glClearColor(color.r, color.g, color.b, color.a);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
colorAction.act(delta);
}
如果要更改背景颜色,只需使用:
colorAction.reset();
colorAction.setEndColor(Color.BLUE);
答案 1 :(得分:0)
这是使用插值的一种方法。应该是不言自明的。
private final Color clearColor = new Color();
private final Color startingClearColor = new Color();
private final Color targetClearColor = new Color();
private float elapsedClearColorChangeTime;
private float clearChangeDuration;
private void changeClearColor(int colorHex, float duration){ //for example 0xff0000ff for Red
targetClearColor.set(colorHex);
startingClearColor.set(clearColor);
elapsedClearColorChangeTime = 0;
clearChangeDuration = duration;
}
private void updateClearColor(float deltaTime){
if (elapsedClearColorChangeTime < clearChangeDuration){
elapsedClearColorChangeTime = Math.min(elapsedClearColorChangeTime + deltaTime, clearChangeDuration);
clearColor.set(startingClearColor).lerp(targetClearColor,
Interpolation.fade.apply(elapsedClearColorChangeTime / clearChangeDuration));
}
Gdx.gl.glClearColor(clearColor.r, clearColor.g, clearColor.b, clearColor.a);
}
public void render(float deltaTime){
updateClearColor(deltaTime);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
//...
}