我在LibGDX中做了简单的应用。我有两个纹理(背景和 Sprite )。背景显示正常,但我看不到 Sprite 。我尝试了很多解决方案,但没有任何效果。我不知道发生了什么事。感谢您的帮助。
这是我的代码:
public void create() {
float w = Gdx.graphics.getWidth();
float h = Gdx.graphics.getHeight();
Texture.setEnforcePotImages(false);
camera = new OrthographicCamera(1, h/w);
batch = new SpriteBatch();
texture = new Texture(Gdx.files.internal("graphic/bg1.png"));
bad = new Texture(Gdx.files.internal("graphic/b.png"));
layerOne = new TextureRegion(texture);
spriteb = new Sprite(bad);
rbg = new ParallaxBackground(new ParallaxLayer[]{
new ParallaxLayer(layerOne, new Vector2(),new Vector2()),
}, w, h,new Vector2());
}
public void render() {
Gdx.gl.glClearColor(1, 1, 1, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
rbg.render();
camera.update();
batch.setProjectionMatrix(camera.combined);
batch.begin();
spriteb.setPosition(400, 200);
spriteb.draw(batch);
batch.end();
}
最佳答案
您的相机视口(viewport)对于 Sprite 位置来说太小
camera = new OrthographicCamera(1, h/w);
...
spriteb.setPosition(400, 200);
Sprite 正在远离视口(viewport)的地方渲染。
尝试像这样设置你的相机:
camera = new OrthographicCamera(w, (h/w)*h);
关于java - Sprite 不显示在 Libgdx 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20301097/