提前为这个非常新手的问题道歉,目前正在尝试学习 libGDX,每个教程都表明这应该可行,它正确地在屏幕中央显示文本按钮,但没有显示背景图像 title.png。
我花了很多时间尝试让它工作,但没有运气,您能给我的任何帮助将不胜感激!
public class MainMenu implements Screen {
ZebraGems game;
Stage stage;
BitmapFont font;
BitmapFont blackfont;
TextureAtlas atlas;
Skin skin;
SpriteBatch batch;
TextButton button;
Texture titleTexture;
Sprite titleSprite;
public MainMenu(ZebraGems game){
this.game = game;
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
stage.act(delta);
batch.begin();
titleSprite.draw(batch);
stage.draw();
batch.end();
}
@Override
public void resize(int width, int height) {
if (stage == null);
stage = new Stage (width, height, true);
stage.clear();
titleTexture = new Texture("data/title.png");
titleSprite = new Sprite(titleTexture);
titleSprite.setColor(1,1,1,0);
titleSprite.setSize(480, 800);
titleSprite.setPosition (0, 0);
Gdx.input.setInputProcessor(stage);
TextButtonStyle style = new TextButtonStyle();
style.up = skin.getDrawable("play");
style.down = skin.getDrawable("playpressed");
style.font = blackfont;
button = new TextButton("", style);
button.setWidth(213);
button.setHeight(94);
button.setX(Gdx.graphics.getWidth() /2 - button.getWidth() /2);
button.setY(Gdx.graphics.getHeight() /2 - button.getHeight() /2);
stage.addActor(button);
}
@Override
public void show() {
batch = new SpriteBatch();
atlas = new TextureAtlas("data/playButton.pack");
skin = new Skin();
skin.addRegions(atlas);
blackfont = new BitmapFont(Gdx.files.internal("data/blackfont.fnt"));
}
@Override
public void hide() {
dispose();
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
batch.dispose();
skin.dispose();
atlas.dispose();
stage.dispose();
blackfont.dispose();
}
}
最佳答案
将 stage.draw()
移出 batch.begin()
和 batch.end()
。
此外,如果您使用的是最新的 nighty 版本的 libgdx,您应该将 stage.getViewport().update(width, height, true)
添加到您的 resize(...)
方法中。
此外,您正在执行此 titleSprite.setColor(1,1,1,0)
操作,将背景 Sprite 的 alpha 设置为 0,这意味着 100% 透明 = 不可见。
请执行以下操作:titleSprite.setColor(1,1,1,1)
。
总的来说,你的渲染应该是这样的:
batch.begin();
titleSprite.draw(batch);
batch.end();
stage.act(delta);
stage.draw();
关于java - 为什么这不显示我的背景图片 title.png?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22857985/