Removing element from ArrayList every 500 frames

后端 未结 2 1789
独厮守ぢ
独厮守ぢ 2020-12-12 02:53

I have this arraylist:

// Add predators
predators = new ArrayList();
for (int i = 0; i < predNum; i++) {
  Creature predator = new Creature(random(width),         


        
2条回答
  •  难免孤独
    2020-12-12 03:29

    If you already have a variable that keeps track of what frame you are on, you can use this if statement:

    if (frameCount % 500 == 0) {
       predators.remove(1); //use this if you want to remove whatever is at index 1 every 500 frames
       predators.remove(predators.size() -1); //use this if you want to remove the last item in the ArrayList
    }
    

    Since you used 1 as the argument for the remove method of the ArrayList, I did too, but note that this will always remove the 2nd object in the arrayList since arrayList indices start counting at 0.

    This will only run every time the framecount is a multiple of 500.

    If you do not already keep track of the frameCount you will have to put frameCount++ in the loop that is executed every frame.

提交回复
热议问题