private Array<Rectangle> livinglamas;
我希望这个数组的每个矩形都包含一个整数。这个整数应该在spawnLama()中指定,这样每个矩形都包含它自己的值。我该怎么做?
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/