private Array<Rectangle> livinglamas;
我希望此数组为每个矩形包含一个整数。应该在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();
...
最佳答案
对其进行子类化,并使用子类而不是Rectangle:
public class RectangleWithInt extends Rectangle {
public int value;
}
或使用Libgdx的ArrayMap。与Java的Map不同,您可以有重复的键,并且像Array一样,它是有序的:
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;
//...
}
关于java - 创建一个包含RECTANGLE和INTEGER的数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/35039310/