我是libdgx的新手。
我想创建一个带有背景图像的按钮,我用gdx-texturepacker.jar制作了图像包,然后将其加载到TextureAtlus中,但是现在无论我运行我的程序,它都会出现此错误

没有使用名称注册com.badlogic.gdx.scenes.scene2d.ui.TextButton $ TextButtonStyle:默认

我的密码

package com.me.mygdxgame;

import com.badlogic.gdx.ApplicationListener;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.audio.Music;
import com.badlogic.gdx.audio.Sound;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.ui.Button;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;


public class Desperate implements ApplicationListener {

SpriteBatch batch;
int soundcheck=0;
Music music;
Actor background;
Actor red_player;

Button b;
TextureAtlas atlas;
Skin skin;

int x;
int y;

@Override
public void create() {

    x = Gdx.app.getGraphics().getWidth();
    y = Gdx.app.getGraphics().getHeight();

    batch = new SpriteBatch();
    music = Gdx.audio.newMusic(Gdx.files.internal("data/open.wav"));

    background = new Actor("data/green_surface.png",

    Gdx.app.getGraphics().getWidth(),
Gdx.app.getGraphics().getHeight(),0,0);
    red_player = new Actor("data/red2.png", 100,150,230,120);
    atlas = new TextureAtlas(Gdx.files.internal("ui/button.pack"));
    skin = new Skin(atlas);
    b = new Button(skin);

}

@Override
public void dispose() {

    red_player.dispose();
    batch.dispose();
}

@Override
public void render() {


    Gdx.gl.glClearColor(1, 1, 1, 1);
    Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);


    if(Gdx.input.isTouched()){


        red_player.position.x+=1f;

red_player.actor.setPosition(red_player.position.x,red_player.position.y);

    }



    batch.begin();
    //batch.draw(mario,0,0);
    background.actor.draw(batch);
    red_player.actor.draw(batch);
    b.draw(batch, 0);
    //front.draw(batch);

    batch.end();

    if(soundcheck==0)
    {

        try {
            Thread.sleep(2000);
        } catch(InterruptedException ex) {
            Thread.currentThread().interrupt();
        }
        //music.play();
        //sound.play(1f);
        soundcheck=1;
        System.out.println("Sound played!");
    }

}

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


}

@Override
public void pause() {
}

@Override
public void resume() {
}
}


请告诉我该怎么办,或者如果我走的路不对,请告诉我在屏幕上绘制按钮的最简单方法是什么

最佳答案

b = new Button(skin);


是相同的

b = new Button(skin,"default");


因此,它从.json外观文件中寻找样式定义,看来您没有

com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle: {
    default: { up: btn_default_normal, down: btn_default_pressed, over: btn_default_focused, disabled: btn_default_disabled, font: default-font, fontColor: text-<col> , downFontColor: text-<col>, overFontColor: text-<col>, disabledFontColor: text-<col>},


似乎如果您像这里一样在不使用skinFile参数的情况下初始化外观,则...

 skin = new Skin(atlas);


...那么您将无法将Skin作为参数传递给scene2d.ui元素构造函数。那些具有Skin参数的构造函数假定您具有此skinFile,并且它只是为了方便在初始化时使用“默认”样式或其他某种样式。

如果您的皮肤只有TextureAtlas,则仍然可以执行以下操作:

skin.getDrawable("mypackedimage");


大多数情况下,尽管我认为其目的是在运行时动态构建您的Skin,而不是持久化.json方法进行皮肤设置。

您应该通过Google搜索如何制作自己的皮肤文件,以及LibGDX论坛上的一些免费文件。

关于android - 没有使用名称注册com.badlogic.gdx.scenes.scene2d.ui.TextButton $ TextButtonStyle:默认,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20033205/

10-10 01:56