更改下一个元素时,将覆盖ArrayList / List中的Java先前元素 [英] Java previous elements in ArrayList/List being overwritten when changing next element

查看:121
本文介绍了更改下一个元素时,将覆盖ArrayList / List中的Java先前元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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, the position for all the elements is equivalent to the value in the last element. I debugged through this, but can figure out why this is occuring.

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

edit: the function below is where I am initializing and populating the options. It also contains the logic to set the position for each option.

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

我的对象元素被声明为Button的通用列表像下面的对象。我不确定这是否有所作为。

My objects element is declared as a generic list of Button objects like below. I am not sure if that makes a difference.

List<Button> options = new ArrayList<Button>();

编辑:我有一个类是按钮,它有两个重要对象:PositionedTexture背景和PositionedText文本。每个对象都有一个位置的Vector2。每个类的代码都在下面

edit: I have one class that is button that has two important objects: PositionedTexture background and PositionedText text. Each of those objects has a Vector2 for a position. The code for each class is below

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


推荐答案

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

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

这篇关于更改下一个元素时,将覆盖ArrayList / List中的Java先前元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆