I am a little new to Java. In forLoop below, I iterate over the elements of an array, and I'm trying to change the position object. When forLoop is complete, the position for all elements is equivalent to the value in the last element. I debugged this, but I can understand why this is happening.
edit: the function below is where I initialize and populate the parameters. It also contains 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);
}
}
The my objects element is declared as a general list of Button objects, as shown below. I'm not sure if that matters.
List<Button> options = new ArrayList<Button>();
edit: I have one class, which is a button that has two important objects: PositionedTexture background and PositionedText. Each of these objects has Vector2 for the 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) {
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);
font.setColor(0, 0, 0, 1);
font.draw(batch, text.Text, text.Position.x, text.Position.y);
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();
}
}