-1

I can't figure out why I'm not able to see my thouchpad that I've created, but it works. Here is the code

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();
    }
}

And in my Create class I use this code for adding it into the stage

        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);

And in the render method this code

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

1 Answers1

1

There seems to be a problem with your getTouchpadStyle method: it initializes touchpadStyle but instead of returning it, it returns a new and empty style object. In other words, Replace

return new TouchpadStyle();

with

return touchpadStyle;
asherbret
  • 5,439
  • 4
  • 38
  • 58