【问题标题】:libgdx background image not displayinglibgdx 背景图像不显示
【发布时间】:2016-08-19 14:49:36
【问题描述】:

我在构造函数中有:

backgroundTexture = new Texture(Gdx.files.internal("Pantalla.jpg"));

在render()中:

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        camera.update();
        myGame.batch.setProjectionMatrix(camera.combined);
        myGame.batch.begin();
        myGame.batch.draw(backgroundTexture, 0, 0);
        stage.draw();
        myGame.batch.end();
        }

类的完整代码为:

    package states;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;

import mx.itesm.mty.MyGame;


public class MenuState implements Screen{
    final MyGame myGame;
    Skin skin;
    Skin skinPant;
    Stage stage;
    SpriteBatch batch;
    TextButton.TextButtonStyle textButtonStyle1;
    TextButton.TextButtonStyle textButtonStyle2;
    TextButton.TextButtonStyle textButtonStyle3;
    TextButton.TextButtonStyle textButtonStyle4;
    TextureAtlas buttonAtlas;
    BitmapFont font;
    Texture backgroundTexture;
    Sprite backgroundSprite;

    OrthographicCamera camera;
    public MenuState(final MyGame game) {

        myGame = game;
        camera = new OrthographicCamera();
        camera.setToOrtho(false,800,480);
        batch = new SpriteBatch();
        skin = new Skin();
        font = new BitmapFont();
        stage = new Stage();
        Gdx.input.setInputProcessor(stage);

        buttonAtlas = new TextureAtlas(Gdx.files.internal("boton.atlas"));
        skin.addRegions(buttonAtlas);



        backgroundTexture = new Texture(Gdx.files.internal("Pantalla.jpg"));
        backgroundSprite = new Sprite(backgroundTexture);

        textButtonStyle1 = new TextButton.TextButtonStyle();
        textButtonStyle1.font = font;
        textButtonStyle1.down = skin.getDrawable("Inicio");
        textButtonStyle1.up = skin.getDrawable("Inicio");
        textButtonStyle1.checked = skin.getDrawable("Inicio");
        TextButton buttonPlay = new TextButton("", textButtonStyle1);

        textButtonStyle2 = new TextButton.TextButtonStyle();
        textButtonStyle2.font = font;
        textButtonStyle2.down = skin.getDrawable("Instrucciones");
        textButtonStyle2.up = skin.getDrawable("Instrucciones");
        textButtonStyle2.checked = skin.getDrawable("Instrucciones");
        TextButton buttonInst = new TextButton("",textButtonStyle2);

        textButtonStyle3 = new TextButton.TextButtonStyle();
        textButtonStyle3.font = font;
        textButtonStyle3.down = skin.getDrawable("Informacion");
        textButtonStyle3.up = skin.getDrawable("Informacion");
        textButtonStyle3.checked = skin.getDrawable("Informacion");
        TextButton buttonInfo = new TextButton("",textButtonStyle3);

        textButtonStyle4 = new TextButton.TextButtonStyle();
        textButtonStyle4.font = font;
        textButtonStyle4.down = skin.getDrawable("Salir");
        textButtonStyle4.up = skin.getDrawable("Salir");
        textButtonStyle4.checked = skin.getDrawable("Salir");
        TextButton buttonSalir = new TextButton("",textButtonStyle4);

        buttonPlay.setWidth(300f);
        buttonPlay.setHeight(100f);
        buttonPlay.setPosition(Gdx.graphics.getWidth() / 2 - 150f, Gdx.graphics.getHeight() / 2);

        buttonInst.setWidth(300f);
        buttonInst.setHeight(100f);
        buttonInst.setPosition(Gdx.graphics.getWidth() / 2 - 150f, Gdx.graphics.getHeight() / 2 - 120f);

        buttonInfo.setWidth(300f);
        buttonInfo.setHeight(100f);
        buttonInfo.setPosition(Gdx.graphics.getWidth() / 2 - 150f, Gdx.graphics.getHeight() / 2 - 240f);

        buttonSalir.setWidth(300f);
        buttonSalir.setHeight(100f);
        buttonSalir.setPosition(Gdx.graphics.getWidth() / 2 - 150f, Gdx.graphics.getHeight() / 2 - 360f);

        buttonPlay.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                myGame.setScreen(new PlayMenuState(myGame));
            }
        });
        buttonInst.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                myGame.setScreen(new PlayMenuState(myGame));
            }
        });
        buttonInfo.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                myGame.setScreen(new PlayMenuState(myGame));
            }
        });
        buttonSalir.addListener(new ChangeListener() {
            @Override
            public void changed(ChangeEvent event, Actor actor) {
                myGame.setScreen(new PlayMenuState(myGame));
            }
        });
        stage.addActor(buttonPlay);
        stage.addActor(buttonInst);
        stage.addActor(buttonInfo);
        stage.addActor(buttonSalir);
    }
    @Override
    public void show() {
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(0, 0, 0.2f, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        camera.update();
        myGame.batch.setProjectionMatrix(camera.combined);
        myGame.batch.begin();
        myGame.batch.draw(backgroundTexture, 0, 0);
        stage.draw();
        myGame.batch.end();
        }

    @Override
    public void resize(int width, int height) {

    }

    @Override
    public void pause() {

    }

    @Override
    public void resume() {

    }

    @Override
    public void hide() {

    }

    @Override
    public void dispose() {

    }
}

我添加了几个按钮和背景图片。图片在 assets 文件夹中,但没有显示出来。

我对矩形有疑问,如果有一种方法可以像设置 x、y、高度、宽度一样将图像添加到 Rectangle 对象?

【问题讨论】:

  • 你为什么叫myGame.batch而不是你在MenuState类中声明的那个?顺便说一句,别忘了接受你之前question的答案
  • 忘记了!谢谢!我打电话给myGame 因为我声明了myGame = game
  • 但是在那种情况下,你为什么要在这个类中再次声明SpriteBatch batch;?而且我看到你有backgroundSprite,那为什么不画呢?
  • 我更正了关于myGame.batch 的问题,我只收到了班级收到的game.batch。我启动game.batch.begin();,然后调用myGame.batch.draw(backgroundTexture, 0, 0, 800, 480); stage.draw();,图像仍然没有出现。如果我取出stage.draw(); 并出现
  • 解决方案。如果有人遇到此问题,请将stage.draw(); 移出game.batch.begin()

标签: java android android-studio libgdx


【解决方案1】:

Stage 有它自己的批处理,它将调用 begin() 和 end(),除非您专门设置舞台使用相同的批处理,否则 begin() 和 end() 调用会发生冲突。

不过,您可能还想清理代码,因为您创建了一个背景精灵但不使用它。我建议使用它而不是直接绘制纹理(因为精灵也可以有一个位置和其他你可以操作的属性)。

【讨论】:

  • 是的,我确实在另一个论坛上发现了这一点,但无论如何谢谢,直到现在我才完全明白为什么。所以最好使用精灵?
  • 嗯,它使您的代码更灵活,更容易更改背景。基本上,精灵是纹理的容器,它还具有位置旋转、缩放、大小等。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2018-04-18
  • 2010-12-19
  • 2012-10-16
  • 2012-12-11
  • 2016-06-11
  • 2015-12-10
  • 2019-02-27
相关资源
最近更新 更多