Java previous elements in ArrayList/List being overwritten when changing next element

前端 未结 2 346
后悔当初
后悔当初 2021-01-19 06:14

I am a bit new to Java. In the forLoop below, I am looping over the elements of the arraylist and I am trying to change the position object. When the forLoop is finished, th

相关标签:
2条回答
  • 2021-01-19 06:37

    Ahhhh I see the problem... It is in the class PositionedTexture. You are not creating a new Position for each Button. So basically all the Buttons position point to the Vector2.Zero.

    Creating a new Position element is the way to go...

    public class PositionedTexture {
    public Texture Texture;
    public Vector2 Position;
    
    public PositionedTexture(String texturePath) {
        Texture = new Texture(Gdx.files.internal(texturePath));
        Position = Vector2.Zero;
    }
    
    public PositionedTexture(String texturePath, Vector2 position) {
        Texture = new Texture(Gdx.files.internal(texturePath));
        Position = position;
    }
    
    0 讨论(0)
  • 2021-01-19 06:49

    Make sure that you didn't put the same instance of the object in all positions of the array list - in other words add the same element over and over again. That's what would probably cause the behavior you're seeing.

    0 讨论(0)
提交回复
热议问题