世界 class 不显示我的背景
World class doesn't display my background
我有一个名为 World 的 class,其中包含与玩家互动的所有实体(障碍物、背景、场景)和这个 class有一个名为 drawWorld() 的方法,可以在其中绘制所有实体。这个方法我第一个放的是背景,但是没有画背景贴图,不知道为什么。
此渲染方法来自我的游戏屏幕以及我调用方法 drawWorld():
@Override
public void render(float delta) {
// OpenGL clear screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(Gdx.gl.GL_COLOR_BUFFER_BIT | Gdx.gl.GL_DEPTH_BUFFER_BIT);
// Set the projection matrix for the SpriteBatch
this.game.spriteBatch.setProjectionMatrix(this.game.orthoCamera.combined);
// Act stage
this.game.stage.act(delta);
// SpriteBatch begins
this.game.spriteBatch.begin();
// Draw world
this.world.drawWorld();
// Draw stage
this.game.stage.draw();
// SpriteBatch ends
this.game.spriteBatch.end();
}
这里我渲染背景:
public void drawWorld() {
// Draw background
this.game.spriteBatch.draw(this.background, 0, 0);
}
别忘了我创建了背景纹理:
// Load background image
this.background = Assets.manager.get(Assets.background);
我做错了什么?
当您创建一个只有 ViewPort
的 Stage
时,在幕后,Stage
creates and uses it's own Batch
。因此,在您的 render
方法中,您 game.spritBatch
绘制背景,然后 Stage
的 Batch
在 [=20= 之间绘制舞台 ] 和 end
,共 game.spriteBatch
。这是一个很大的不不。
应该这样做的方式是这样的:
正在创建 Stage
:
this.game.stage = new Stage(this.viewPort, this.game.spriteBatch); // For efficiency, you can use the same spriteBatch
Render
:
@Override
public void render(float delta) {
// OpenGL clear screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(Gdx.gl.GL_COLOR_BUFFER_BIT | Gdx.gl.GL_DEPTH_BUFFER_BIT);
// Set the projection matrix for the SpriteBatch
this.game.spriteBatch.setProjectionMatrix(this.game.orthoCamera.combined);
// Act stage
this.game.stage.act(delta);
// SpriteBatch begins
this.game.spriteBatch.begin();
// Draw world
this.world.drawWorld();
// SpriteBatch ends
this.game.spriteBatch.end();
// Draw stage
this.game.stage.draw(); // After spriteBatch.end()!!
}
有关 Stage(Viewport viewport, Batch batch)
构造函数的更多信息,请参阅 docs。
我有一个名为 World 的 class,其中包含与玩家互动的所有实体(障碍物、背景、场景)和这个 class有一个名为 drawWorld() 的方法,可以在其中绘制所有实体。这个方法我第一个放的是背景,但是没有画背景贴图,不知道为什么。
此渲染方法来自我的游戏屏幕以及我调用方法 drawWorld():
@Override
public void render(float delta) {
// OpenGL clear screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(Gdx.gl.GL_COLOR_BUFFER_BIT | Gdx.gl.GL_DEPTH_BUFFER_BIT);
// Set the projection matrix for the SpriteBatch
this.game.spriteBatch.setProjectionMatrix(this.game.orthoCamera.combined);
// Act stage
this.game.stage.act(delta);
// SpriteBatch begins
this.game.spriteBatch.begin();
// Draw world
this.world.drawWorld();
// Draw stage
this.game.stage.draw();
// SpriteBatch ends
this.game.spriteBatch.end();
}
这里我渲染背景:
public void drawWorld() {
// Draw background
this.game.spriteBatch.draw(this.background, 0, 0);
}
别忘了我创建了背景纹理:
// Load background image
this.background = Assets.manager.get(Assets.background);
我做错了什么?
当您创建一个只有 ViewPort
的 Stage
时,在幕后,Stage
creates and uses it's own Batch
。因此,在您的 render
方法中,您 game.spritBatch
绘制背景,然后 Stage
的 Batch
在 [=20= 之间绘制舞台 ] 和 end
,共 game.spriteBatch
。这是一个很大的不不。
应该这样做的方式是这样的:
正在创建 Stage
:
this.game.stage = new Stage(this.viewPort, this.game.spriteBatch); // For efficiency, you can use the same spriteBatch
Render
:
@Override
public void render(float delta) {
// OpenGL clear screen
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(Gdx.gl.GL_COLOR_BUFFER_BIT | Gdx.gl.GL_DEPTH_BUFFER_BIT);
// Set the projection matrix for the SpriteBatch
this.game.spriteBatch.setProjectionMatrix(this.game.orthoCamera.combined);
// Act stage
this.game.stage.act(delta);
// SpriteBatch begins
this.game.spriteBatch.begin();
// Draw world
this.world.drawWorld();
// SpriteBatch ends
this.game.spriteBatch.end();
// Draw stage
this.game.stage.draw(); // After spriteBatch.end()!!
}
有关 Stage(Viewport viewport, Batch batch)
构造函数的更多信息,请参阅 docs。