声明: 我是LibGDX的新手,对java有一个相当基本的了解。我已经使用谷歌和Stackoverflow搜索答案,但无济于事。对不起英语,我不是母语人士。
这是我的问题:
我想为游戏创建一些按钮,为了保存代码,我正在尝试创建一个自定义的TextButton类。但是,我遇到了问题。
到目前为止,这是我的代码:
package com.mygame.game;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;
import com.badlogic.gdx.scenes.scene2d.ui.TextButton;
public class customButton extends TextButton{
TextButtonStyle customButtonStyle;
BitmapFont font;
TextureAtlas buttonAtlas;
public customButton(String text, Skin skin, int x, int y) {
super(text, skin);
customButtonStyle = new TextButtonStyle();
customButtonStyle.font = font;
customButtonStyle.up = skin.getDrawable("accept-button");
this.setStyle(customButtonStyle);
this.setPosition(x, y);
}
}
我用这段代码调用它:
customButton testlol;
...
skin = new Skin();
buttonAtlas = new TextureAtlas(Gdx.files.internal("icons"));
skin.addRegions(buttonAtlas);
testlol = new customButton("Testhallo", skin, 200, 200);
stage.addActor(testlol);
等等。但是,我收到了一个错误:
Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: No com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle registered with name: default
...
at com.mygame.game.customButton.<init>(customButton.java:17)
错误引用了这一行:
super(text, skin);
(我删除了一些冗余错误文本)
我看不出什么是基本错误。什么是我的(基本错误在这里?:/
答案 0 :(得分:2)
您正在调用的超级构造函数super(text, skin);
尝试使用默认样式实例化TextButton。因此,您的皮肤必须具有已为TextButtons定义的默认样式。或者,您可以从超类的构造函数中复制并粘贴代码,而不是调用它,并根据需要调整它以跳过尝试应用默认样式。