【问题标题】:World class doesn't display my background世界级不显示我的背景
【发布时间】:2015-06-29 21:51:53
【问题描述】:

我有一个名为 World 的类,其中包含与玩家交互的所有实体(障碍物、背景、场景),并且该类有一个名为 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);

我做错了什么?

【问题讨论】:

  • 在屏幕中引用了 game 对象:this.game.spriteBatch.begin()world 对象:this.game.spriteBatch.draw(this.background, 0, 0)。是同一个game 对象吗?
  • 抱歉,延迟发布,但是是的,它是同一个游戏对象。
  • 你记得在调用this.background = Assets.manager.get(Assets.background);之前加载Assets.background吗?
  • 在 Game 类中我做了 Assets.load() 和 Assets.manager.finishLoading()。在我的播放器类中,我做了 this.texture = Assets.manager.get(Assets.character),所以它也应该在我的 World 类中工作。你想要更多的代码来看看发生了什么吗?
  • 是的。你能具体说明一下this.game.stage是如何初始化的吗?

标签: background libgdx textures render


【解决方案1】:

当您创建一个只有ViewPortStage 时,在后台,Stage creates and uses it's own Batch。因此,在您的render 方法中,您有game.spritBatch 绘制背景,然后StageBatchbegingame.spriteBatch 之间的end 之间绘制舞台 >。这是一个很大的不。

应该这样做的方式是这样的:

创建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

【讨论】:

  • 谢谢!!有效。如果我要做一个新游戏,我总是需要将 stage.draw() 放在 spriteBatch.end() 之后?
  • 是的。如果您查看Stagesource,您会看到draw 调用batch.beginbatch.end。您不能在不同的 batch.beginbatch.end 之间这样做。
猜你喜欢
  • 2021-12-28
  • 1970-01-01
  • 2018-08-06
  • 2010-12-19
  • 1970-01-01
  • 2014-02-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多