简体   繁体   中英

Libgdx error loading atlas file

I've started to develop game in Libgdx and I started to work on something. I got a background.png I'm trying to load from my skin. I'm trying to load gameskin.atlas into my AssetManager object but everytime I try to load it, I get an error message:

Caused by: com.badlogic.gdx.utils.SerializationException: Error parsing file: packs/gameskin.atlas
    at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:77)
    at com.badlogic.gdx.utils.Json.fromJson(Json.java:692)
    ... 14 more
Caused by: com.badlogic.gdx.utils.SerializationException: Error parsing JSON on line 3 near: 
gameskin.png
*ERROR*format: RGBA8888
filter: Nearest,Nearest
repeat: none
footballpi
    at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:549)
    at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:55)
    at com.badlogic.gdx.utils.JsonReader.parse(JsonReader.java:75)
    ... 15 more

Here is where I try to load it:

package screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;

import engine.Values;


public class GameScreen extends ScreenWrapper{

    private final String GAME_SKIN_PATH = "packs/gameskin.atlas";
    private ScreenRenderer renderer;
    public GameScreen(ScreenManager manager) {
        super(manager);
    }

    @Override
    public void initiate() {
        manager.getLoader().load(GAME_SKIN_PATH, Skin.class);
    }

    @Override
    public void buildStage() {
        Skin skin = manager.getLoader().getAsset(GAME_SKIN_PATH, Skin.class);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(242/255f, 242/255f, 242/255f, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        super.render(delta);
    }

    @Override
    public void dispose() {
        manager.getLoader().unload(GAME_SKIN_PATH);
    }

}

I tried to delete the size parameter in the gameskin.atlas file but it's not the problem. What could be the problem?

EDIT: I tried to use AssetDescriptors and SkinParameter to solve this problem. Now it emits this error:

Exception in thread "LWJGL Application" com.badlogic.gdx.utils.GdxRuntimeException: com.badlogic.gdx.utils.GdxRuntimeException: Error reading pack file: packs/gameskin.atlas
    at com.badlogic.gdx.assets.AssetManager.handleTaskError(AssetManager.java:570)
    at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:375)
    at com.badlogic.gdx.assets.AssetManager.finishLoading(AssetManager.java:396)
    at engine.utils.AssetsLoader.load(AssetsLoader.java:41)
    at screens.GameScreen.initiate(GameScreen.java:21)
    at screens.ScreenManager.pushScreen(ScreenManager.java:21)
    at engine.TapTapMain.create(TapTapMain.java:27)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:147)
    at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Error reading pack file: packs/gameskin.atlas
    at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:187)
    at com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:58)
    at com.badlogic.gdx.assets.loaders.TextureAtlasLoader.getDependencies(TextureAtlasLoader.java:34)
    at com.badlogic.gdx.assets.AssetLoadingTask.handleSyncLoader(AssetLoadingTask.java:99)
    at com.badlogic.gdx.assets.AssetLoadingTask.update(AssetLoadingTask.java:88)
    at com.badlogic.gdx.assets.AssetManager.updateTask(AssetManager.java:498)
    at com.badlogic.gdx.assets.AssetManager.update(AssetManager.java:373)
    ... 7 more
Caused by: com.badlogic.gdx.utils.GdxRuntimeException: Invalid line: 
    at com.badlogic.gdx.graphics.g2d.TextureAtlas.readTuple(TextureAtlas.java:443)
    at com.badlogic.gdx.graphics.g2d.TextureAtlas$TextureAtlasData.<init>(TextureAtlas.java:115)
    ... 13 more

Here is my GameScreen Now:

package screens;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.Skin;

import engine.Values;
import engine.utils.AssetsLoader;


public class GameScreen extends ScreenWrapper{

    private ScreenRenderer renderer;
    // The constructor gets the ScreenManager to notify on screen transitions
    public GameScreen(ScreenManager manager) {
        super(manager);
    }

    @Override
    public void initiate() {
        manager.getLoader().load(AssetsLoader.SKIN_GAME);
    }

    @Override
    public void buildStage() {
        Skin skin = manager.getLoader().findAsset(AssetsLoader.SKIN_GAME);
        Image backgroundPitch = new Image(skin.getDrawable("football_pitch"));
        backgroundPitch.setSize(Values.WIDTH, Values.HEIGHT);
        renderer.addActor(backgroundPitch);
        addActor(renderer);
    }

    @Override
    public void render(float delta) {
        Gdx.gl.glClearColor(242/255f, 242/255f, 242/255f, 0);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);
        super.render(delta);
    }

    @Override
    public void dispose() {
        manager.getLoader().unload(Values.GAME_SKIN_PATH);
    }

}

You are trying to load a Atlas into a Skin . A Atlas is a file that holds all the packed data of a image. A Skin holds the data to use with Scene2D elements like BitmapFont , LabelStyle and ButtonStyle .

A skin needs both a json skin file and a atlas. To load it properly in one go with a AssetManager you need to use the SkinLoader.SkinParameter .

manager.load("skin.json", Skin.class, new SkinLoader.SkinParameter("image.atlas"));

In addition, I prefer to use AssetDescriptors to lookup assets in the manager. These will go into your manager class:

//A atlas or texture do not require additional parameters.
public static final AssetDescriptor<TextureAtlas> ATLAS = 
    new AssetDescriptor<TextureAtlas>("image.atlas", TextureAtlas.class);

//A skin does require additional parameters.
public static final AssetDescriptor<Skin> SKIN = 
    new AssetDescriptor<Skin>("skin.json", Skin.class, 
    new SkinLoader.SkinParameter("image.atlas"));

Now load these when/however you want manager.load(skin) . When you need a specific asset you can look these descriptors up staticaly in your Assets class and pass it to your manager manager.find(Assets.SKIN) . Firstly this creates a nice list of all your assets when you type Assets. , secondly you don't need to cast or add the .class of the asset when your assigning it since that is already in the AssetDescriptor and finally you don't need a string to lookup a path in the manager which is error prone.

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