I have this arraylist:
// Add predators
predators = new ArrayList();
for (int i = 0; i < predNum; i++) {
Creature predator = new Creature(random(width),
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<Creature> predators = new ArrayList<Creature>();
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
}
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.