更改下一个元素时,ArrayList/List 中的 Java 前一个元素被覆盖

Ken*_*old 4 java list arraylist

我对Java有点陌生。在下面的 forLoop 中,我循环遍历 arraylist 的元素,并尝试更改位置对象。当 forLoop 完成时,所有元素的位置等于最后一个元素中的值。我对此进行了调试,但可以弄清楚为什么会发生这种情况。

编辑:下面的函数是我初始化和填充选项的地方。它还包含为每个选项设置位置的逻辑。

protected void InitializeOptions() {
    options = new ArrayList<Button>();
    options.add(new Button("button.png", "Quick Fire"));
    options.add(new Button("button.png", "20 Questions"));
    options.add(new Button("button.png", "Decisions! Decisions!"));
    options.add(new Button("button.png", "OMG"));

    for(int i = 0; i < OPTIONCOUNT; ++i) {
        options.get(i).SetPosition(i*35, i*35);
    }
}
Run Code Online (Sandbox Code Playgroud)

我的对象元素被声明为一个通用的 Button 对象列表,如下所示。我不确定这是否有所作为。

List<Button> options = new ArrayList<Button>();
Run Code Online (Sandbox Code Playgroud)

编辑:我有一个按钮类,它有两个重要的对象:PositionedTexture 背景和 PositionedText 文本。这些对象中的每一个都有一个用于位置的 Vector2。每个类的代码如下

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;
    }
}

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;
    }
}

public class Button {
    protected PositionedTexture background;
    protected PositionedText text;
    protected Vector2 center;
    protected Vector2 scale;
    protected float rotation;
    protected Rectangle rect;
    protected ShapeRenderer shapeRenderer;

    public Button(String spritePath, String btnText) {
        background = new PositionedTexture(spritePath);
        text = new PositionedText(btnText);
        rect = new Rectangle(6, 41, 498, 171);
        center = new Vector2((rect.width - rect.x)/2, (rect.height - rect.y)/2);
        scale = new Vector2(0.5f,0.5f);
        rotation = 0.0f;
        shapeRenderer = new ShapeRenderer();
    }

    public Button(String spritePath, String btnText, Vector2 pos) {
        background = new PositionedTexture(spritePath, pos);
        text = new PositionedText(btnText, pos);
        rect = new Rectangle(6, 41, 498, 171);
        center = new Vector2((rect.width - rect.x)/2, (rect.height - rect.y)/2);
        scale = new Vector2(0.5f,0.5f);
        rotation = 0.0f;
        shapeRenderer = new ShapeRenderer();
    }

    public void SetPosition(Vector2 pos) {
        background.Position = pos;
        text.Position = pos;
    }

    public void SetPosition(float x, float y) {
        background.Position.x = x;
        background.Position.y = y;
        text.Position.x = x;
        text.Position.y = y;
    }

    public void Draw(SpriteBatch batch, BitmapFont font) {
        // draw background
        batch.draw(background.Texture, background.Position.x, 
                   background.Position.y, center.x, center.y,
                   background.Texture.getWidth(), background.Texture.getHeight(),
                   scale.x, scale.y, rotation, (int)rect.x, (int)rect.y,
                   (int)rect.width, (int)rect.height, false, false);

        // draw text
        font.setColor(0, 0, 0, 1);
        font.draw(batch, text.Text, text.Position.x, text.Position.y);

        // draw collision rect
        shapeRenderer.begin(ShapeType.Line);
        shapeRenderer.setColor(1, 0, 0, 1);
        shapeRenderer.rect(background.Position.x, background.Position.y,
                           background.Texture.getWidth() * scale.x,
                           background.Texture.getHeight() * scale.y);
        shapeRenderer.end();
    }
}
Run Code Online (Sandbox Code Playgroud)

Gur*_*ran 5

Ahhhh 我看到了问题......它在类 PositionedTexture 中。您不是为每个按钮创建一个新位置。所以基本上所有的按钮位置都指向 Vector2.Zero。

创建一个新的 Position 元素是要走的路......

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;
}
Run Code Online (Sandbox Code Playgroud)