Reputation: 187
private Array<Rectangle> livinglamas;
I want this Array to contain an integer for every rectangle. This integer should be specified in spawnLama() so every Rectangle contains its own value. How do I do this?
private void spawnLama() {
Rectangle livinglama = new Rectangle();
livinglama.x = MathUtils.random(-800, -400 - 64);
livinglama.y = 0;
livinglama.width = 64;
livinglama.height = 64;
livinglamas.add(livinglama);
lastLamaTime = TimeUtils.nanoTime();
}
and
@Override
public void render() {
...
elapsedTime += Gdx.graphics.getDeltaTime();
if(TimeUtils.nanoTime() - lastLamaTime > 1000000000L) spawnLama();
Iterator<Rectangle> iter = livinglamas.iterator();
while(iter.hasNext()) {
Rectangle livinglama = iter.next();
livinglama.x += LamaXBewegung * Gdx.graphics.getDeltaTime();
if(livinglama.y + 64 < -575) iter.remove();
}
batch.begin();
for(Rectangle livinglama: livinglamas) {
batch.draw(animation.getKeyFrame(elapsedTime, true), livinglama.x, livinglama.y);
}
elapsedTime += Gdx.graphics.getDeltaTime();
...
Upvotes: 0
Views: 265
Reputation:
Just create some wrapper to tie rectangle and integer:
public class DataHolder {
public final Rectangle rect;
public final int i;
public DataHolder(Rectangle rect, int i) {
this.rect = rect;
this.i = i;
}
}
then create array or arraylist of dataHolders:
// array of dataholders (fixed size)
DataHolder[] arr = new DataHolder[size];
arr[0] = new DataHolder(someRectangle, someInteger);
// arraylist of dataholders (dynamically expandable size)
ArrayList<DataHolder> arrlist = new ArrayList<>();
arrlist.add(new DataHolder(someRectangle, someInteger));
Upvotes: 0
Reputation: 93739
Subclass it and use the subclass instead of Rectangle:
public class RectangleWithInt extends Rectangle {
public int value;
}
Or use Libgdx's ArrayMap. Unlike Java's Map, you can have duplicate keys, and like Array, it is ordered:
private ArrayMap<Rectangle, Integer> livinglamas;
//...
livinglamas.put(livinglama, someInt);
//...
Iterator<Entry<Rectangle, Integer>> iter = livinglamas.iterator();
while (iter.hasNext()){
Entry<Rectangle, Integer> entry = iter.next();
Rectangle lama = entry.key;
int value = entry.value;
//...
}
Upvotes: 1
Reputation: 1396
You probably should make a class called Llama, containing an int
and a Rectangle
:
class Llama
{
public int n;
public Rectangle box = new Rectangle();
}
Then in spawnLama
:
Llama livinglama = new Llama();
livinglama.box.x = MathUtils.random(-800, -400 - 64);
etc.
Upvotes: 0
Reputation: 1494
I'm not sure what you're trying to do exactly, but I think what you're looking for is either a minimal wrapper class for Rectangle
containing an int id:
public class RectangleWrapper {
private int id;
private Rectangle rectangle;
//getters and setters
}
or a map (if you're not concerned with the ordering of the collection you can use HashMap<>
):
Map<Integer, Rectangle> livinglamas = new HashMap<Integer, Rectangle>();
livinglamas.put(1, new Rectangle());//for example
Upvotes: 1
Reputation: 3548
You can do something like this:
private Map<Rectangle, Integer> rectByInt;
Upvotes: 1