Search code examples
javaandroidlibgdxinstances

Why are instances overwriting each other in LibGDX?


So I have this game I'm making with LibGDX. I have a GroundTop and Ground class that draws the ground and ground top on the screen (image loading in an Assets class), and will eventually apply collisions with the player (this is a platformer). What I'm trying to make it do is draw 4 different Ground instances, and 2 different GroundTop instances, for testing. But, there is only one of each. What it seems like is that each instance overwrites the other, since the only ones showing up have the coordinates of the last instances created in the code. Why is this, and how can I make it so each instance is displayed? Is there something that makes each instance overwrite the other in the classes? I am completely lost on this. Thanks in advance, and please ask any questions you have to help clarify my problem and help me find a solution :)

Here's my code:

Game.java

package com.mygdx.thetimetraveller;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.utils.Array;

public class Game implements Screen {
    public static int frameCount;

    Array<Ground> groundList = new Array<Ground>();
    Ground[] grounds = new Ground[1000];

    GroundTop groundTop;
    GroundTop groundTop2;

    Game() {
        load();
    }

    @Override
    public void load() {
        groundList.add(new Ground(50, 50));
        groundList.add(new Ground(250, 250));
        grounds[0] = new Ground(500, 500);
        grounds[1] = new Ground(650, 650);
        groundTop = new GroundTop(500, 20);
        groundTop2 = new GroundTop(500, 200);
        frameCount = 0;
    }

    @Override
    public void display(SpriteBatch batch) {
        frameCount ++;

        groundList.get(0).Draw(batch);
        groundList.get(1).Draw(batch);

        grounds[0].Draw(batch);
        grounds[1].Draw(batch);

        groundTop.Draw(batch);
        groundTop2.Draw(batch);
    }
}

Ground.java

package com.mygdx.thetimetraveller;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class Ground {
    private static double x;
    private static double y;

    Ground(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public void Draw(SpriteBatch batch) {
        batch.draw(Assets.simpleBlockTexture01_sprite, (float) x, (float) y);
    }
}

GroundTop.java

package com.mygdx.thetimetraveller;

import com.badlogic.gdx.graphics.g2d.SpriteBatch;

public class GroundTop {
    private static double x;
    private static double y;

    GroundTop(double x, double y) {
        this.x = x;
        this.y = y;
    }

    public void Draw(SpriteBatch batch) {
        batch.draw(Assets.simpleBlockTextureTop01_sprite, (float) x, (float) y);
    }
}

Solution

  • private static double x;
    private static double y;
    

    This is your problem. static fields are common for all instances of given class. So your all Ground object share x and y variables. Solution: just remove static modifer from your fields in both Ground and GroundTop classes