简体   繁体   中英

How do I set background color in libgdx?

I am trying to make the background a sky blue color with RGB of 135,206,235. When I run it, the background is not the color I expected.

public void render () {
    Gdx.gl.glClearColor(.135f, .206f, .235f, 1);
    Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    batch.begin();
    batch.draw(img, 0, 0);
    batch.end();
}

glClearColor uses range from 0 to 1, so you need to map from range 0 - 255 by simply dividing by constant 255f:

Gdx.gl.glClearColor(135/255f, 206/255f, 235/255f, 1);

Also be cautious when dividing 2 integers, if you don't convert any of then to float (or double), integer division will be used and result will be 0 (except for 255/255 == 1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM