【问题标题】:Libgdx Touchpad Knob Won't Resize ProperlyLibgdx 触摸板旋钮无法正确调整大小
【发布时间】:2017-03-03 21:04:29
【问题描述】:

我正在 libgdx 中为 android 制作游戏,并且一直致力于实现视口。我有一个 libgdx 触摸板作为舞台上的演员,一旦我将 FitViewport 应用到舞台上,触摸板旋钮不会正确调整大小并填满屏幕,而所有其他元素都会正确调整大小。如何让触摸板旋钮与其他元素一起调整大小?

这是我的代码:

private Touchpad touchpad;
private Touchpad.TouchpadStyle touchpadStyle;
private Skin touchpadSkin;
private Drawable touchBackground;
private Drawable touchKnob;

public PlayState(GameStateManager gsm) {
    super(gsm);
    touchpadSkin = new Skin();
    //Set background image
    touchpadSkin.add("touchBackground", new Texture("joystick_base.png")); 
    //Set knob image
    touchpadSkin.add("touchKnob", new Texture("joystick_64.png"));
    //Create TouchPad Style
    touchpadStyle = new Touchpad.TouchpadStyle();
    //Create Drawable's from TouchPad skin
    touchBackground = touchpadSkin.getDrawable("touchBackground");
    touchKnob = touchpadSkin.getDrawable("touchKnob");
    touchpadStyle.background = touchBackground;
    touchpadStyle.knob = touchKnob;

    //Create new TouchPad with the created style
    touchpad = new Touchpad(10, touchpadStyle);
    //setBounds(x,y,width,height)
    touchpad.setBounds(0.5f,0.5f,WORLD_WIDTH/7,WORLD_HEIGHT/5);


    stage = new Stage(new FitViewport(WORLD_WIDTH,WORLD_HEIGHT));
    stage.addActor(touchpad);
    Gdx.input.setInputProcessor(stage);

protected void render(SpriteBatch sb) {
    touchpad.draw(sb, 1);
}

【问题讨论】:

    标签: java android libgdx


    【解决方案1】:

    尝试在 resize 方法上更新视口:

     stage.getViewport().update(width, higth);
    

    【讨论】:

    • 如何覆盖resize方法?
    • 如果您使用 intellij IDEA,请右键单击代码,然后按 Generate... 并触摸覆盖方法。如果没有,只需输入 public void resize(float width, float high) {}
    • 这段代码在一个扩展 State 的 PlayState 类中,所以我不能覆盖 resize 方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多