渲染循环条件LIBGDX

时间:2014-04-30 09:17:07

标签: java loops libgdx render conditional-statements

我正在慢慢学习java/LIBGDX,我有以下方法在点击时从黑/白更改图像,然后我在渲染中调用touchGem方法。

我在缠绕我的头部时遇到的问题是在渲染中调用此方法以在创建新的黑色或白色宝石时停止渲染(正如您在点击它时无疑会告诉它渲染图像一遍又一遍。)

public void touchGem() {

    Gdx.input.setInputProcessor(stage);

    if (touchGemStatus1 < 1) {
        touchGem1 = new Image(touchGemTextureWhite);
        touchGem1.setColor(1, 1, 1, 1);
        touchGem1.setSize(95, 95);
        touchGem1.setPosition(5, 0);
        touchGem1.addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y,
                    int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y,
                    int pointer, int button) {
                if (touchGemStatus1 < 1) {
                    touchGemStatus1 = touchGemStatus1 + 1;

                } else {
                    touchGemStatus1 = touchGemStatus1 - 1;
                }
            }
        });
    } else {
        touchGem1 = new Image(touchGemTextureBlack);
        touchGem1.setColor(1, 1, 1, 1);
        touchGem1.setSize(95, 95);
        touchGem1.setPosition(5, 0);
        touchGem1.addListener(new InputListener() {
            public boolean touchDown(InputEvent event, float x, float y,
                    int pointer, int button) {
                return true;
            }

            public void touchUp(InputEvent event, float x, float y,
                    int pointer, int button) {
                if (touchGemStatus1 < 1) {
                    touchGemStatus1 = touchGemStatus1 + 1;

                } else {
                    touchGemStatus1 = touchGemStatus1 - 1;

                }
            }
        });

    }
}

1 个答案:

答案 0 :(得分:2)

如果render()方法只调用touchGem(),它根本不会渲染任何东西。要绘制touchGem1,您的render()方法应如下所示:

    @Override
public void render(float deltaTime) {

    deltaTime = Gdx.graphics.getDeltaTime();

    touchGem();

    batch.begin();
    stage.addActor(touchGem1);
    batch.end();
    stage.act(deltaTime);
    stage.draw();

}