2016-01-27 78 views
0
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(); 

...

+2

如何使用Map ? –

回答

1

继承它,并使用子类,而不是矩形:

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; 
    //... 
} 
+0

谢谢!我使用了ArrayMap。但这仍然不行:\t for(Rectangle lama:livinglamas){ \t \t \t batch.draw(animation.getKeyFrame(elapsedTime,true),lama.x,lama.y); \t \t}我需要在那里改变什么? – AlGrande

+0

@AlGrande如果你已经将'livinglamas'改为'ArrayMap <>'然后迭代它就好,如果这是你的问题 – jonk

+0

把'(Rectangle lama:livinglamas)'改为'for(Rectangle lama :livinglamas.keys())' – Tenfour04

1

你可以做这样的事情:

private Map<Rectangle, Integer> rectByInt; 
1

我不知道你想要做什么,但我认为你在寻找什么或者是包含一个int ID Rectangle最小包装类:

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 
0

你或许应该做一个叫骆驼类,包含intRectangle

class Llama 
{ 
    public int n; 
    public Rectangle box = new Rectangle(); 
} 

然后在spawnLama

Llama livinglama = new Llama(); 

livinglama.box.x = MathUtils.random(-800, -400 - 64); 

0

只要创建一些包装,以配合矩形和整数:

public class DataHolder { 
    public final Rectangle rect; 
    public final int i; 

    public DataHolder(Rectangle rect, int i) { 
     this.rect = rect; 
     this.i = i; 
    } 
} 

然后创建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));