0

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);
    }
}
  • 1
    thetimetraveller, now I'm curious about the Game. :) – Daniele Sep 26 '15 at 21:54
  • 1
    Haha, well alright then. So, long story short, the player finds a time machine, uses it, and accidentally spills some drink on it. Then he/she has to go through many time zones to get back home. I plan to make it so the user can choose where to go in time at certain times. Glad to see you're interested :D – Shivashriganesh Mahato Sep 26 '15 at 21:58
  • Cool! Good luck, I cannot wait to play it! – Daniele Sep 26 '15 at 22:03

2 Answers2

3
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

wasyl
  • 3,421
  • 3
  • 27
  • 36
1

I would say that this is more related to Java than to libGDX itself.

Sometimes in Java you want to have variables that are common to all objects. This is accomplished with the static modifier. Fields that have the static modifier in their declaration are called static fields or class variables. They are associated with the class, rather than with any object. Every instance of the class shares a class variable, which is in one fixed location in memory. Any object can change the value of a class variable, but class variables can also be manipulated without creating an instance of the class.

To learn more about check this link.

Daniele
  • 1,332
  • 1
  • 13
  • 24