Removing element from ArrayList every 500 frames

后端 未结 2 1771
独厮守ぢ
独厮守ぢ 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:22

    The draw() function is called 60 times per second, so that's the loop you would be using. The frameCount variable is incremented automatically each time draw() is called.

    Like The Coding Wombat said, you can use the modulo operator to determine when a variable (like frameCount) is a multiple of a value (like 500).

    You can combine those ideas to do something once ever 500 frames:

    ArrayList predators = new ArrayList();
    
    void setup(){
      for (int i = 0; i < predNum; i++) {
        Creature predator = new Creature(random(width), random(height), 2);
        predators.add(predator);
      }
    }
    
    void draw(){
      if (frameCount % 500 == 0){
       predators.remove(predators.size()-1);
      }
    
      //draw your frame
    }
    

提交回复
热议问题