private Array<Rectangle> livinglamas;
我希望这个Array包含每个矩形的整数。应该在spawnLama()中指定此整数,以便每个Rectangle都包含自己的值。我该怎么做?
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();
}
和
@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();
...
答案 0 :(得分:1)
您可以这样做:
private Map<Rectangle, Integer> rectByInt;
答案 1 :(得分:1)
我不确定你要做的是什么,但我认为你要找的是Rectangle
包含int id的最小包装类:
public class RectangleWrapper {
private int id;
private Rectangle rectangle;
//getters and setters
}
或地图(如果您不关心可以使用HashMap<>
的集合的顺序):
Map<Integer, Rectangle> livinglamas = new HashMap<Integer, Rectangle>();
livinglamas.put(1, new Rectangle());//for example
答案 2 :(得分:1)
对它进行子类化并使用子类而不是Rectangle:
// 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));
或者使用Libgdx的ArrayMap。与Java的Map不同,您可以拥有重复的键,并且像Array一样,它是有序的:
public class RectangleWithInt extends Rectangle {
public int value;
}
答案 3 :(得分:0)
你可能应该创建一个名为Llama的类,其中包含int
和Rectangle
:
class Llama
{
public int n;
public Rectangle box = new Rectangle();
}
然后在spawnLama
:
Llama livinglama = new Llama();
livinglama.box.x = MathUtils.random(-800, -400 - 64);
等
答案 4 :(得分:0)
只需创建一个包装矩形和整数的包装器:
NerdTree
然后创建dataHolders的数组或arraylist:
public class DataHolder {
public final Rectangle rect;
public final int i;
public DataHolder(Rectangle rect, int i) {
this.rect = rect;
this.i = i;
}
}