LIBGDX - 触摸板

时间:2016-01-28 17:58:15

标签: libgdx touchpad

我无法弄清楚为什么我无法看到我创建的胸垫,但它有效。这是代码

package com.mygdx.game;

import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.Touchpad;
import com.badlogic.gdx.scenes.scene2d.utils.Drawable;


public class AnalogStick extends Touchpad {

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

    public AnalogStick(float x, float y) {

        super(10, getTouchpadStyle());
        setBounds(15, 15, 200, 200);
        setPosition(x,y);

    }

    private static Touchpad.TouchpadStyle getTouchpadStyle() {

        touchpadSkin = new Skin();
        touchpadSkin.add("touchBackground", new Texture("touchBackground.png"));

        touchpadSkin.add("touchKnob", new Texture("touchKnob.png"));

        touchpadStyle = new Touchpad.TouchpadStyle();

        touchBackground = touchpadSkin.getDrawable("touchBackground");
        touchKnob = touchpadSkin.getDrawable("touchKnob");

        touchpadStyle.background = touchBackground;
        touchpadStyle.knob = touchKnob;

        return new TouchpadStyle();
    }
}

在我的Create类中,我使用此代码将其添加到舞台

        asMove = new AnalogStick(15,15);

        Gdx.input.setInputProcessor(stage);

        playerTexture = new Texture(Gdx.files.internal("player.png"));
        playerSprite = new Sprite(playerTexture);

        stage = new Stage(new ScreenViewport(), batch);
        stage.addActor(asMove);
        Gdx.input.setInputProcessor(stage);

在渲染方法中这段代码

        stage.act(Gdx.graphics.getDeltaTime());
        stage.draw();

1 个答案:

答案 0 :(得分:1)

您的getTouchpadStyle方法似乎存在问题:它初始化touchpadStyle但不返回它,而是返回一个新的空style对象。换句话说,替换

return new TouchpadStyle();

return touchpadStyle;