【问题标题】:Rendering a scene2d ui on top of box2d在box2d之上渲染scene2d ui
【发布时间】:2014-03-27 15:18:48
【问题描述】:

我目前在 box2d 游戏区域上渲染 UI 时遇到问题。截至目前,该按钮呈现在屏幕的角落,但不会对点击做出反应,因为似乎屏幕认为它不存在。只有它的图片。下面是屏幕工作原理的代码。正在呈现什么顺序。在 spritebatch 开始和结束之前和之后的凸轮缩放、更新和设置是导致精灵在角落的测试,但它仍然没有做任何事情。我很困惑,希望能得到一些关于如何以一种好的方式处理渲染的建议。

public synchronized void render(float delta) {
    getCamControll().update();

    Gdx.gl.glClearColor(0, 0, 0, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);

    // RENDER SPRITES
    batch.setProjectionMatrix(cam.combined);
    batch.begin();

    Iterator<Renderable> itr = renderables.iterator();
    while (itr.hasNext()) {
        Renderable r = itr.next();
        r.render(batch);
    }

    batch.end();

    // RENDER DEBUG
    sr.setProjectionMatrix(cam.combined);
    sr.begin(ShapeType.Line);
    for (Renderable r : renderables) {
        r.debug(sr);
    }
    sr.end();

    // RENDER GUI
    float camzoom = cam.zoom;
    Vector3 camPos = cam.position.cpy();
    cam.zoom = 1;
    cam.position.set(Vector2.Zero, cam.position.z);
    cam.update();
    batch.setProjectionMatrix(new Matrix4());
    if (stage != null) {
        batch.begin();
        stage.draw();
        Array<Actor> actors = stage.getActors();
        batch.end();
    }

    cam.zoom = camzoom;
    cam.position.set(camPos);
    cam.update();
}

【问题讨论】:

    标签: user-interface camera libgdx box2d scene2d


    【解决方案1】:

    您需要正确设置输入处理程序,即在您的设置/初始化代码中的某处(在您创建阶段之后)您需要:

        Gdx.input.setInputProcessor(stage);
    

    如果 box2d(或其他任何东西)除了场景之外还需要处理输入,您应该查看 Libgdx 的 InputMultiplexer 来组合输入处理程序。

    【讨论】:

    • Box2D 对输入处理甚至渲染一无所知。它是一个纯物理引擎,甚至可以在 C++ 中运行,甚至不是 Java :)
    • 好的,谢谢,我自己还没用过Box2D。那我贴的代码应该够用了。
    • @noone 但如果他需要输入处理器来移动他的Sprites(我认为是这种情况),他需要设置一个InputMultiplexer
    • @Springrbua 如果他想要两个不同的输入处理程序,那么他将需要一个多路复用器,是的。但 Box2D 肯定不需要。
    • 我很困惑。输入多路复用器将如何提供帮助?我希望 UI 跟随屏幕,但 box2d 不这样做。从而在顶部呈现带有按钮的 UI。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2013-10-21
    • 1970-01-01
    • 1970-01-01
    • 2019-01-13
    相关资源
    最近更新 更多