Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Libgdx-在游戏世界中添加按钮/图像控件?_Java_Android_User Interface_Libgdx - Fatal编程技术网

Java Libgdx-在游戏世界中添加按钮/图像控件?

Java Libgdx-在游戏世界中添加按钮/图像控件?,java,android,user-interface,libgdx,Java,Android,User Interface,Libgdx,我对libgdx还是一个新手,我很困惑,我想在我的游戏中有一些按钮,通常在左下角的屏幕上,用于制作一些事件使用物品、创建士兵等。我可以把这个按钮放在我的游戏类中的什么位置?我有worldController类、worldRenderer类和GameScreen类,就像libgdx电子书中的canyon bunny一样 我曾尝试在worldRenderer类中添加stage和button,然后在renderGui方法中绘制它,但没有显示任何内容。那么,如何在我的游戏世界中插入按钮呢? 谢谢 pub

我对libgdx还是一个新手,我很困惑,我想在我的游戏中有一些按钮,通常在左下角的屏幕上,用于制作一些事件使用物品、创建士兵等。我可以把这个按钮放在我的游戏类中的什么位置?我有worldController类、worldRenderer类和GameScreen类,就像libgdx电子书中的canyon bunny一样 我曾尝试在worldRenderer类中添加stage和button,然后在renderGui方法中绘制它,但没有显示任何内容。那么,如何在我的游戏世界中插入按钮呢? 谢谢

public class WorldRenderer implements Disposable {
    private OrthographicCamera camera;
    public SpriteBatch batch;
    private Stage stage = new Stage();
    private Button btnItem;
    private Skin skinLibgdx = new Skin(
            Gdx.files.internal(Constants.SKIN_LIBGDX_UI),
            new TextureAtlas(Constants.TEXTURE_ATLAS_LIBGDX_UI));
    private WorldController worldController;
    private OrthographicCamera cameraGUI;
    public float w = Constants.VIEWPORT_GUI_WIDTH/1366;
    public float h = Constants.VIEWPORT_GUI_HEIGHT/768;
    public WorldRenderer (WorldController worldController) { 
        this.worldController = worldController;
        init();
    }



    private void init () {
        batch = new SpriteBatch();
        camera = new OrthographicCamera(Constants.VIEWPORT_WIDTH, Constants.VIEWPORT_HEIGHT);
        camera.position.set(0, 0, 0);
        camera.update();
        btnItem = new Button(skinLibgdx);
        btnItem.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                // TODO Auto-generated method stub
                System.out.println("ABC");
            }
        });
        stage.addActor(btnItem);
        //background = new ParallaxBackground(layers,camera,batch);
        cameraGUI = new OrthographicCamera(Constants.VIEWPORT_GUI_WIDTH,
        Constants.VIEWPORT_GUI_HEIGHT);
        cameraGUI.position.set(0, 0, 0);
        cameraGUI.setToOrtho(true); // flip y-axis
        cameraGUI.update();
    }
    public void render () { 
        renderWorld(batch);
        renderGui(batch);
    }

    private void renderWorld (SpriteBatch batch) {
        //background.render();
        worldController.cameraHelper.applyTo(camera);
        batch.setProjectionMatrix(camera.combined);
        batch.begin();
        worldController.level.render(batch);
        batch.end();

    }

    public void resize (int width, int height) { 
        //camera.viewportWidth = (Constants.VIEWPORT_HEIGHT / height) *width;
        camera.update();

        cameraGUI = new OrthographicCamera(Constants.VIEWPORT_GUI_WIDTH,
        Constants.VIEWPORT_GUI_HEIGHT);
        cameraGUI.position.set(0, 0, 0);
        cameraGUI.setToOrtho(true); // flip y-axis
        cameraGUI.update();
    }

    @Override
    public void dispose() {
        // TODO Auto-generated method stub
        batch.dispose();
    }

    private void renderGuiScore (SpriteBatch batch) {
        float x = -15;
        float y = -15;
        batch.draw(Assets.instance.enemies.soldiers,x, y, 50, 50, 100, 100, 0.35f, -0.35f, 0);
        Assets.instance.fonts.defaultBig.draw(batch,
                "" + worldController.score,
                x + 75, y + 37);
        //stage.draw(); i have try draw in this line too but UI become dissapear
    }

    private void renderGuiExtraLive (SpriteBatch batch) {
        float x = cameraGUI.viewportWidth - 50 - Constants.LIVES_START*50;
        float y = -15*h;
        for (int i = 0; i < Constants.LIVES_START; i++) {
            if (worldController.lives <= i)
            batch.setColor(0.5f, 0.5f, 0.5f, 0.5f);
            batch.draw(Assets.instance.hero.hero1,
            x + i * 50, y, 50, 50, 120, 100, 0.35f, -0.35f, 0);
            batch.setColor(1, 1, 1, 1);
        }
    }

    private void renderGuiHealth (SpriteBatch batch) {
        float x = 50;
        float y = -15*h;
        for (int i = 0; i < Constants.LIVES_START; i++) {
            if (worldController.lives <= i)
            batch.setColor(Color.RED);
            float health = worldController.level.hero.getHealth();
            Assets.instance.fonts.defaultBig.draw(batch,
                    "" + Math.round(health),
                    x + 75, y + 37);
        }
    }

    private void renderGuiEnergy (SpriteBatch batch) {
        float x = 125;
        float y = -15*h;
        for (int i = 0; i < Constants.LIVES_START; i++) {
            if (worldController.lives <= i)
            batch.setColor(Color.GREEN);
            float energy = worldController.level.hero.getEnergy();
            Assets.instance.fonts.defaultBig.draw(batch,
                    "" + Math.round(energy),
                    x + 75, y + 37);
        }
    }

    private void renderGuiPlasma (SpriteBatch batch) {
        float x = 200;
        float y = -15*h;
        for (int i = 0; i < Constants.LIVES_START; i++) {
            if (worldController.lives <= i)
            batch.setColor(Color.BLACK);
            Assets.instance.fonts.defaultBig.draw(batch,
                    "" + worldController.level.hero.getUltimateEnergy() + "-" + worldController.level.hero.getPower() + "-" + worldController.plasma,
                    x + 75, y + 37);

        }
    }

    private void renderGui (SpriteBatch batch) {
        batch.setProjectionMatrix(cameraGUI.combined);
        batch.begin();
        // draw collected gold coins icon + text
        // (anchored to top left edge)
        renderGuiScore(batch);
        // draw extra lives icon + text (anchored to top right edge)
        renderGuiExtraLive(batch);
        //draw Health
        renderGuiHealth(batch);
        //draw Energy
        renderGuiEnergy(batch);
        //draw Plasma
        renderGuiPlasma(batch);
        batch.end();
        stage.draw();
    }
}
你忘了:

stage.act();
并设置输入处理器:

Gdx.input.setInputProcessor(stage);