LibGdx How to program an HP Bar?

你。 提交于 2019-12-22 09:24:01

问题


I am currently trying to program a game using LibGdx. I have a lot of the structure part of my game and I am now seeking to have the game return information to the player. The simplest concept for my UI I can think of are HP bars and ammo counters (for bullets, arrows, etc.). Would this be done through a stage and actors in my GameScreen class? Perhaps in the Rendering class? Or is this a second camera (loaded-in after the background)?

I use a GameScreen class supported by a Map and MapRenderer class to achieve my display.

I will be looking to pick apart the LibGdx tests. Any information to get my feet off the ground would be good!


回答1:


I used this code some time ago (untested for now). It uses just two ninepatches drawn on top of each other. You can use the default skin of libgdx (uiskin from the test resources).

public class HealthBar extends Actor {

    private NinePatchDrawable loadingBarBackground;

    private NinePatchDrawable loadingBar;

    public HealthBar() {
        TextureAtlas skinAtlas = new TextureAtlas(Gdx.files.internal("data/uiskin.atlas"));
        NinePatch loadingBarBackgroundPatch = new NinePatch(skinAtlas.findRegion("default-round"), 5, 5, 4, 4);
        NinePatch loadingBarPatch = new NinePatch(skinAtlas.findRegion("default-round-down"), 5, 5, 4, 4);
        loadingBar = new NinePatchDrawable(loadingBarPatch);
        loadingBarBackground = new NinePatchDrawable(loadingBarBackgroundPatch);
    }

    @Override
    public void draw(Batch batch, float parentAlpha) {
        float progress = 0.4f;

        loadingBarBackground.draw(batch, getX(), getY(), getWidth() * getScaleX(), getHeight() * getScaleY());
        loadingBar.draw(batch, getX(), getY(), progress * getWidth() * getScaleX(), getHeight() * getScaleY());
    }
}

Keep in mind that you should use an AssetManager to retrieve the atlas texture instead of creating a new Atlas like this.



来源:https://stackoverflow.com/questions/21892694/libgdx-how-to-program-an-hp-bar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!