MAGS94
MAGS94

Reputation: 510

Using a value from json file in Libgdx

I want to use a value from a json file

Here is the Json file

{
  "button" : [
    {
      "x" : 50.0
    },
    {
      "x" : 150.0
    }
]
}

I have the following classes

(Button Class)

public class Button extends Sprite{

    float x;

    public Button() {
        super(new Texture("button.png"));
    }

    @Override
    public void setX(float x) {
        this.x = x;
    }

}

(Data Class)

public class Data {

    public Array<Button> buttons;

    public void load() {
        buttons = new Array<Button>();

        Json json = new Json();
        json.setTypeName(null);
        json.setUsePrototypes(false);
        json.setIgnoreUnknownFields(true);
        json.setOutputType(JsonWriter.OutputType.json);
        json.fromJson(Data.class, Gdx.files.internal("buttons.json"));

    }
}

(Main Class)

public class GameMain extends ApplicationAdapter {

    SpriteBatch batch;
    Data data;

    @Override
    public void create () {
        batch = new SpriteBatch();

        data = new Data();
        data.load();

        for(Button b : data.buttons) {
            b.setX(b.x);
        }

    }

    @Override
    public void render () {
        Gdx.gl.glClearColor(0, 0, 0, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        batch.begin();
        for(Button b : data.buttons) {
            b.draw(batch);
        }
        batch.end();
    }
}

I want to draw buttons in specific x positions that is held in json file but it gives me nothing .

What is wrong in my code ?

Any ideas ?

Upvotes: 1

Views: 411

Answers (2)

Tenfour04
Tenfour04

Reputation: 93882

Your Data class loads another instance of a Data class and doesn't assign it to anything. It's circular and doesn't make sense. The load method should be static, return a Data object (which is what the last line of your current load method provides), and not be trying to instantiate an empty buttons array that goes unused.

Your button class hides both the x field and the setX method of the superclass, making it impossible to change the actual X position of the sprite that is used when it is drawn. Sprite already has an x parameter, so you should not be adding your own. If you merely remove those two things from your Button class, it should work.

That said, you should not be loading another copy of the same texture for each button. That's a waste of memory and texture swapping. And unless you are very careful about disposing the textures "owned" by these sprites, you are also leaking memory.

Upvotes: 1

Gavriel
Gavriel

Reputation: 19247

at the end of load() you haven't asign the results. Just add buttons = :

public void load() {
    //instead of this line:
    //buttons = new Array<Button>();

    Json json = new Json();
    json.setTypeName(null);
    json.setUsePrototypes(false);
    json.setIgnoreUnknownFields(true);
    json.setOutputType(JsonWriter.OutputType.json);
    // set buttons here:
    buttons = json.fromJson(Data.class, Gdx.files.internal("buttons.json"));

}

Upvotes: 1

Related Questions