简体   繁体   中英

libGDX - Black buttons and text after changing screen

I've recently discovered a strange problem in my libGDX program. When I start my program I first show a SplashScreen , then the MainMenu screen, and then you can enter the program. The problem is, that if I change screen while I'm on the main menu, all of my TextButton s become completely black, and their text characters become black boxes. 切换画面之前

切换画面后

(I reloaded the MainMenu using the splash button)

This affects all TextBoxes in all other screens. My thoughts is that my JSON is faulty, in some way. Because when I stop using drawables from my JSON and instead use FreeTypeFont instead of my BitmapFont , at least the font no longer turns black. I've used validators and they say there's no problem with my JSON, but here it is anyways ( TextButtonStyle at the bottom):

{
"com.badlogic.gdx.graphics.Color": {
    "white": {
        "r": 1,
        "g": 1,
        "b": 1,
        "a": 1
    },

    "black": {
        "r": 0,
        "g": 0,
        "b": 0,
        "a": 1
    },

    "red": {
        "r": 1,
        "g": 0,
        "b": 0,
        "a": 1
    },

    "green": {
        "r": 0,
        "g": 1,
        "b": 0,
        "a": 1
    },

    "blue": {
        "r": 0,
        "g": 0,
        "b": 1,
        "a": 1
    },

    "dark_grey": {
        "r": 0.8,
        "g": 0.8,
        "b": 0.8,
        "a": 1
    }
},

"com.badlogic.gdx.graphics.g2d.BitmapFont": {
    "white": {
        "file": "data/fonts/white.fnt"
    },

    "black": {
        "file": "data/fonts/black.fnt"
    }
},

"com.badlogic.gdx.scenes.scene2d.ui.Window$WindowStyle": {
    "default": {
        "titleFont": "black",
        "titleFontColor": "dark_grey"
    }
},

"com.badlogic.gdx.scenes.scene2d.ui.Label$LabelStyle": {
    "default": {
        "font": "white", 
        "fontColor": "white"
    }
},

"com.badlogic.gdx.scenes.scene2d.ui.List$ListStyle": {
    "default": {
        "font": "white",
        "fontColorUnselected": "white",
        "fontColorSelected": "black",
        "selection": "default.selection"
    }
},

"com.badlogic.gdx.scenes.scene2d.ui.ScrollPane$ScrollPaneStyle": {
    "default": {
        "hScrollKnob": "button.up",
        "vScrollKnob": "button.up"
    }
},

"com.badlogic.gdx.scenes.scene2d.ui.TextButton$TextButtonStyle": {
    "default": {
        "up": "button.up",
        "down": "button.down",
        "font": "black",
        "pressedOffsetX": 1,
        "pressedOffsetY": -1
    }
}
}

Here's the code where I add and change the buttons' styles.

    // Splash Screen button
    buttonSplash = new TextButton("SPLASH SCREEN", skin);
    buttonSplash.addListener(new ClickListener() {
        @Override
        public void clicked(InputEvent event, float x, float y) {
            // When you click the button, create a new screen
            ((Game) Gdx.app.getApplicationListener()).setScreen(new SplashScreen());
        }
    });
    buttonSplash.pad(20);
    //Rest of the buttons....

If you feel like you need the entire code I can post that as well, but it's fairly long which is why I refrained from posting it.

INSTANT EDIT: Tried with entirely new JSON (the one that comes with libGDX) and I still have the same problem.

It seems like the problem came from disposing the skin . I use a static Skin object in an external class as my skin, which is used by all classes that need that skin. My problem was that I disposed the skin in one of the screens, which disposed the skin that all other classes were using as well. I solved this by simply creating a dispose(); method in the class that holds all of my skins, and dispose of them when I exit the game instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM