We are about to switch to a new forum software. Until then we have removed the registration on this forum.
The application is pretty self-explanatory. When you click multiple times, some ellipses will be removed before their timer class is up. My guess is it has something to do with how I am looping through the Arrays.
Here is the Code:
ArrayList<KnockArea> ka;
ArrayList<Timer> t;
void setup() {
size(1000, 750);
ka = new ArrayList<KnockArea>();
t = new ArrayList<Timer>();
}
void draw() {
background(255);
for (int i = 0; i < ka.size(); i = i + 1) {
ka.get(i).display();
}
for (int i = 0; i < t.size(); i = i + 1) {
if (t.get(i).done) {
t.remove(i);
} else {
t.get(i).update();
}
}
}
void mousePressed() {
ka.add(new KnockArea(mouseX, mouseY, ka.size()));
println("adding new AOE: " + ka.get(ka.size()-1).ID);
}
class KnockArea {
int x;
int y;
boolean doneExpand;
int maxSizeRad;
int sizeRad;
float howLong;
int ID;
float shade;
KnockArea(int xloc, int yloc, int id) {
ID = id;
x = xloc;
y = yloc;
doneExpand = false;
maxSizeRad = 100;
sizeRad = 0;
howLong = 2;
shade = 150;
}
void display() {
noStroke();
fill(shade, shade);
ellipse(x, y, sizeRad*2, sizeRad*2);
if (!doneExpand) {
sizeRad++;
} else {
shade-=1.25;
}
if (sizeRad >= maxSizeRad) { //DONE EXPANDING
t.add(new Timer(howLong, true, ID));
doneExpand = true;
}
}
}
class Timer {
int savedTime;
int totalTime;
int ID;
boolean done;
Timer(float time, boolean seconds, int id) {
if (seconds) {
totalTime = int(time *1000);
}
savedTime = millis();
ID = id;
done = false;
}
void update() {
int passedTime = millis() - savedTime;
if (passedTime > totalTime) {
for (int i = 0; i < ka.size(); i = i + 1) {
if (ka.get(i).ID == ID) {
ka.remove(i);
}
}
done = true;
}
}
}
Answers
Check the delete/remove example (and comments) here: https://processing.org/reference/ArrayList.html
kf
kf, I tried that but the problem is still occurring.
New Code:
One problem is that you are looping through the ArrayList forward and using remove.
Don't do that. As the example explains, loop backwards.
https://processing.org/reference/ArrayList.html
Jeremy, I just did that, is that the problem?
So you add a timer after the circle ends expanding, but then you removethe circle... do you remove the timer object associated to that circle that you remove?
I suggest you define a timer object inside your KnockArea class.
Kf
I should only have one class instead of two?
Notice
and then you have:
So you have two solutions. Either remove the timer object at the same time that you are removing the associated ka.get(i) object (it should have the same index - i). A more elegant solution is to encapsulate your timer class. When you remove your KnockArea object, it will also remove the timer. You can still have two classes. You can make the timer class an inner class of the KnockArea class.
Kf
I've got a more streamlined Countdown class here: O:-)
https://Forum.Processing.org/two/discussion/23846/time-delay-in-python-mode#Item_11
Thanks!
Here's the final code if anyone is interested
Thanks so much for sharing your solution, @Techaterex