简体   繁体   中英

Cycling through arrayList

Background

I have a sprite (an openGL quad) which 'falls' from the top of the screen to the bottom.

When it gets to the bottom of the screen, it's values get reset and it starts again (from top to bottom) but moves along in the X Axis to a new X Position.

WhatI'm doing is storing the X positions in an ArrayList (there could be just 1 so it falls in the same place over and over or 10 so it moves slightly every time it falls).

Problem

What I can't work out is how to go through this arraylist and set the sprite's new X position to the value held in the 'next' arraylist position while bearing in mind that there might not be a 'next' position.

So....... (and this is Pseudo code)....once sprite has fallen and it's Y position has been reset back to the top of the screen:

spriteX = value_held_in_next_arrayList_position(if_there_is_one)

As different levels of my game will have a different number of positions I can't explicitly say 'go to position 2' incase the arrayList is only a size of 1. Basically what I want is to say 'go to next position' and 'if there isn't a next position' then go back to the position 0.

ArrayList is iterable, so this is a good place to use an iterator . Assuming that you're not modifying the list while you're iterating.

You can iterate a List using while with Iterator from the List:

List<YourElement> yourList;
...
Iterator<YourElement> iterator = yourList.iterator();
while(iterator.hasNext()) {
    final YourElement yourElement = iterator.next();        
    // Do whatever with yourElement
    if (iterator.hasNext()) {
        // Do whatever if there's a next element to yourElement
    }
}

What you want to do is get the iterator and then do a while loop where if the iterator has got a next you grab it and carry on, otherwise you come out.

Example code:

Iterator positions = spriteXPositions.iterator();
while(positions.hasNext()) {
    mySprite.position.X = positions.next();
}

I would use an Iterator object. Since you want it to loop and you don't know the size you want to abstract that from you. We can make a generic loop method that takes an iterator and the parent ArrayList and will cycle through it if it is already at the end.

private void loop(Iterator it, ArrayList parent) {
    if(!it.hasNext()) { 
        it = parent.iterator();
    } 
    //Your logic here instead of logging
    //Assume always at least 1 element in arraylist
    Log.d("test", it.next() + "");
}

We can test it:

ArrayList<Float> positions = new ArrayList<Float>();
positions.add(0.123123f);
positions.add(0.2312f);
positions.add(0.323123f);

Iterator<Float> posIter = positions.iterator();
for(int i = 0; i < 100; i++) {
    loop(posIter, positions);
}

You can use the % operator to go back to the beginning of the list list.

int nextSpriteX() {
   currentXPositionIndex = (currentXPositionIndex + 1) % xPositions.size();
   return xPositions.get(currentXPositionIndex);
}

Thanks to all who contributed answers, I went with a different approach in the end, in case anyone has a similar issue, this is what I did:

if (xPosition.get(0)<X.size()-1){

xPosition.set(0, xPosition.get(0)+1);

}

else {xPosition.set(0, 1);
}

sprite.xScreen=xPosition.get(xPosition.get(0));

xPosition is an arraylist and the first element is simply a pointer to another element which contains the X Position - so something like:

xPosition.add(1);  //Element 0 - pointer to subsequent elements (Points to element 1)
xPosition.add(100); //Element 1 - X position 1
xPosition.add(200)l //Element 2 - X position 2
xPosition.add(400); //Elemen 3 - X Position 3

Cheers!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM