免责声明: 我是 LibGDX 的新手,对 java 有相当基本的了解。我已经使用 Google 和 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);
(我删除了一些多余的错误文本)
我看不出哪里出了问题。我的(这里的根本错误是什么?:/
最佳答案
您调用的 super 构造函数 super(text, Skin);
尝试使用默认样式实例化 TextButton。因此,您的皮肤必须已经为 TextButtons 定义了默认样式。或者,您可以从父类(super class)的构造函数中复制并粘贴代码而不是调用它,并根据需要进行调整以跳过尝试应用默认样式。
关于java - 创建自定义 TextButton 类的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30445781/