android - 没有使用名称 : default 注册的 com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle

标签 android libgdx

我是 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);

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

如果你的皮肤只有 TextureAtlas,你仍然可以这样做:

skin.getDrawable("mypackedimage");

虽然我认为它的主要目的是在运行时动态构建您的皮肤,而不是持久的 .json 皮肤方法。

你应该用谷歌搜索如何制作你自己的皮肤文件,在 LibGDX 论坛上也有一些免费的。

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

相关文章:

java - libGDX 当您使用视口(viewport)并移动相机时如何在屏幕的左上角进行绘制

android - 无法调用异步任务

android - addView() 会为所有 child 调用 onMeasure() 吗?

android - Doze Mode 和 Idle 是一回事吗?

java - libgdx box2d body - 如何通过给定的力或速度、距离和时间移动 body

Android Libgdx : Passing integer, bool 值、字符串等。从 Android 到 Libgdx 的变量值,反之亦然

java - 三星 Galaxy 手机固定为请勿打扰(派)

java - 如何通过反射改变方法行为?

ios - UIActivityViewController 不显示在 iPhone + iOS 8

java - 如何从矩形数组中删除随机矩形?