【问题标题】:LibGdx - Batch is not drawing the last .draw commandLibGdx - 批处理未绘制最后一个 .draw 命令
【发布时间】:2017-11-11 02:16:16
【问题描述】:

所以这很奇怪...... 我有一个抽象的 BaseScreen。此屏幕在 show() 上初始化 SpriteBatch。 init() 调用 BaseScreen 的抽象方法之一。 这个绘制问题来自使用“gameView”视口,它是一个 ExtendViewport

SpriteBatch batch;
@Override
public void show(){
    batch = new SpriteBatch();
    gameView = new ExtendViewport(SV.V_WIDTH, SV.V_HEIGHT);
    hudView = new ScreenViewport();
    init();
}

这是我在渲染函数中所做的:

@Override
public void render(float delta){
    /* 
        Just in case render is being called more than usual 60 times, 
        I want to call update only 60 times per second. (SV.STEP = 1/60f)
     */
    accum += delta;
    while(accum >= SV.STEP){
        accum -= SV.STEP;
        update();
    }

    /* Clear Screen */
    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl20.glClear(GL20.GL_COLOR_BUFFER_BIT);

    /* DRAW WORLD */
    batch.setProjectionMatrix(gameView.getCamera().combined);
    gameView.apply(true);
    batch.begin();
    draw(batch, SV.BatchType.World);
    batch.end();

    /* DRAW UI
    batch.setProjectionMatrix(hudView.getCamera().combined);
    hudView.apply(true);
    batch.begin();
    draw(batch, SV.BatchType.UI);
    batch.end();*/
}

我有两个视口,一个用于游戏世界,一个用于 hud。我用它们来设置批次的投影矩阵。

draw(batch, SV.BatchType.World) 命令最终将代码带到这里:

private void drawBody(Batch batch){
    Vector2 position = body.getPosition();
    //Gdx.app.log(TAG, heart.getWidth() + ", " + heart.getHeight());
    batch.draw(heart, position.x-100,position.y-50, h_width*2, h_height*2);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", 100, 100);
}

现在这段代码绘制了这个:

注意它是如何不绘制文本的。

这是仅使用 batch.draw 的结果 - (font.draw 被省略)

现在问题变成了“最后调用了什么类型的 .draw”,它忽略了所有这些 - 例如:

private void drawBody(Batch batch){
    Vector2 position = body.getPosition();
    //Gdx.app.log(TAG, heart.getWidth() + ", " + heart.getHeight());
    batch.draw(heart, position.x-100,position.y-50, h_width*2, h_height*2);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", 100, 100);
    font.draw(batch, "THIS IS A TEST!", position.x, position.y + 100);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", position.x, position.y - 100);
}

会画

还有这个

private void drawBody(Batch batch){
    Vector2 position = body.getPosition();
    //Gdx.app.log(TAG, heart.getWidth() + ", " + heart.getHeight());
    batch.draw(heart, position.x-100,position.y-50, h_width*2, h_height*2);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", 100, 100);
    font.draw(batch, "THIS IS A TEST!", position.x, position.y + 100);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", position.x, position.y - 100);
    batch.draw(heart, position.x,position.y, h_width*2, h_height*2);
    batch.draw(heart, position.x,position.y+ 100, h_width*2, h_height*2);
}

会画

这是一个有 6 次平局的例子,但你必须有一个“一次性”最后一个 .draw

private void drawBody(Batch batch){
    Vector2 position = body.getPosition();
    //Gdx.app.log(TAG, heart.getWidth() + ", " + heart.getHeight());
    batch.draw(heart, position.x-100,position.y-50, h_width*2, h_height*2);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", 100, 100);
    font.draw(batch, "THIS IS A TEST!", position.x, position.y + 100);
    batch.draw(heart, position.x,position.y, h_width*2, h_height*2);
    batch.draw(heart, position.x,position.y+ 100, h_width*2, h_height*2);
    font.draw(batch, "THIS IS A WOOOOOOOOO!", position.x, position.y - 100);
    batch.draw(heart, 10,10, h_width*2, h_height*2);
}

这是最后一件事...最后一批抽签...如果您尝试在 0, 0 抽签 -> 会发生这种情况

寻求任何有用的反馈和/或可能发生这种情况的原因。我是 LibGdx 的新手,不会回避您对我决定如何使用这个框架的反馈。

非常感谢!

【问题讨论】:

  • 要打印窗口,请按 Alt+PrintScreen 或仅使用截图工具,如截图工具。这样就不会留下那样丑陋的伤口

标签: libgdx


【解决方案1】:

所以我缩小了导致它的原因: 看来你的批次不会被窃听

1) 在 BASE 类上实例化批处理 2)在BASE类渲染方法上调用batch.start 3)在batch.start之后调用BASE类中的抽象方法之一 4) 该批次将被窃听。

我为解决这个问题所做的是将批处理代码移动到派生类中,一切正常。

我仍然愿意回答为什么会出现这个错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-18
    • 1970-01-01
    • 2012-07-29
    • 1970-01-01
    • 2019-09-03
    相关资源
    最近更新 更多