Libgdx触摸板旋钮无法正常调整大小

时间:2017-03-03 21:04:29

标签: java android libgdx

我正在为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);
}

1 个答案:

答案 0 :(得分:0)

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

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