在libGDX中创建一个简单的按钮

问题描述 投票:0回答:1

我是 libgdx 的新手,我花了几个小时努力制作一个简单的按钮!首先我尝试以这种方式生成它(我在这个网站上找到的):

public class TextButtonGenerator {

    public TextButton createButton(String label){
        BitmapFont font = new BitmapFont();
        TextureAtlas buttonAtlas = new TextureAtlas(Gdx.files.internal("sprite.txt"));
        Skin skin = new Skin(buttonAtlas);
//        skin.addRegions(buttonAtlas);
        TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
        textButtonStyle.font = font;
        textButtonStyle.up = skin.getDrawable("button");
        textButtonStyle.down = skin.getDrawable("button");
        textButtonStyle.checked = skin.getDrawable("button");
        return new TextButton(label, textButtonStyle);

我使用 Textpacker 在我的 asset 文件夹中创建 sprites.txt,但是程序仍然无法运行,并且崩溃了。这是我收到的错误消息:

E/AndroidRuntime: FATAL EXCEPTION: GLThread 244475
    Process: com.mygdx.game, PID: 21525
    com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: sprite.txt (Internal)E/AndroidRuntime: FATAL EXCEPTION: GLThread 244475
    Process: com.mygdx.game, PID: 21525
    com.badlogic.gdx.utils.GdxRuntimeException: Error reading file: sprite.txt (Internal)

它说我的代码中问题的根源是我构造TextureAtlas的地方。我认为问题可能是它应该是 .pack 文件而不是 .txt?但在本教程中,他们使用 txt 并且运行良好:

https://www.codeandweb.com/texturepacker/tutorials/libgdx-physicals

我还发现了另一种生成不需要包的按钮的方法:

        Skin skin = new Skin();
        Pixmap pixmap = new Pixmap(1, 1, Pixmap.Format.RGBA8888);
        pixmap.setColor(Color.WHITE);
        pixmap.fill();
        skin.add("white", new Texture(pixmap));
        skin.add("default", new BitmapFont());

        TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
        textButtonStyle.up = skin.newDrawable("white", new Color(0, 0, 0, 1));
        textButtonStyle.font = skin.getFont("default");
        skin.add("default", textButtonStyle);
        return new TextButton(label, skin);

虽然这个没有崩溃但是没有按钮!当我运行它时,我只能在屏幕上看到背景颜色。坦率地说,我什至不理解上面发布的一半代码,我正在尝试学习我的引擎,但感觉我在两天内几乎没有取得任何进展。任何帮助将不胜感激。另外,我将按钮添加到我的舞台,并在相应屏幕的渲染方法中调用 stage.draw() 。

java android android-studio button libgdx
1个回答
0
投票

使用纹理创建按钮

我们可以使用Texture作为按钮。我有一个简单的技巧,它使用一个新类GButton(GameButton)。要制作按钮,只需创建一个新类并命名为GButton,并通过Texture类扩展以继承Texture属性。创建一个构造函数并从 libgdx 库将参数作为 FileHandle 传递。在构造函数主体中写入 super(FileHandle)。 创建一个布尔值方法 isClicked()。 返回 Gdx.input.isTouched();

这是一个简单的代码。

public class GButton extends Texture
{
    GButton(FileHandle s){
        super(s);
    }
    boolean isClicked(){
        return Gdx.input.isTouched();
    }
}

实施我们的新按钮

public class MyGdxGame implements ApplicationListener
{

    GButton b;

    @Override
    public void create()
    {
        b = new GButton(Gdx.files.internal("leftB.png"));
    }

    @Override
    public void render()
    {      
    
        Gdx.gl.glClearColor(1, 1, 1, 1); 
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
    
        batch.begin();
        batch.draw(b,0,0,b.getWidth(),b.getHeight());
        batch.end();
        
        if(b.isClicked()){
            //perform action when button is clicked
        }
}

@Override
public void dispose()
{
}

@Override
public void resize(int width, int height)
{
    
}

@Override
public void pause()
{
}

@Override
public void resume()
{
}


}
© www.soinside.com 2019 - 2024. All rights reserved.