我不知道为什么我看不到我创建的thouchpad,但它有效。这是代码
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();
您的getTouchpadStyle
方法似乎存在问题:它初始化touchpadStyle
但不返回它,而是返回一个新的空style
对象。换句话说,替换
return new TouchpadStyle();
跟
return touchpadStyle;