Those are two nested for loops...right? If so, you are creating the same variable milkpudding twice, once in each for loop. Processing doesn't like when you create different variables with the same name, because it can't tell the two apart. You just need to rename one and call it accordingly. A for loop inside a for loop is called a nested for loop. This is how you would make one (along with y example of changing the name of the variable, you can pick anything, given that its not already taken)
- for(int milkpudding = pudding; milkpudding < 20; milkpudding++) {
- for(int chocolatepudding = pudding + 1; chocolatepudding < 20; chocolatepudding++) {
- //code to run
- }
- }
One thing I would say is that if you are not making a program that relates to pudding, I probably wouldn't choose pudding variables. For the program itself. I would start with a 1-dimensional (regular) array of PVectors, and then toggle through each of the positions in the array twice to look for any two with a distance smaller than the sum of their radii. Since you don't want to be just checking the ones next to any given ellipse, you should probably start with something like this.
If you are not firmiliar with arrays, stop now, and
Read This before I confuse you.
So, you need two for loops, so that for each section of the array (the first for loop), you check every part of the array again (the second for loop). Keeping in mind that the two for loops might read the same circle at the same time, they could/will conflict. So before you try to rip a circle in half, you should check if the two loops are reading the same circle, and if so, ignore the rest of the section. You would need two because the first scrolls you through the PVector array once, and for each position in the array, the second scrolls and compares the array to itself. It works a little like this:
For Loop 1 : For loop 2
----------------------------------------
circles[1] | circles[1]
| circles[2]
| circles[3]
| circles[4]
| circles[5]
circles[2] | circles[1]
| circles[2]
| circles[3]
| circles[4]
| circles[5]
and on and on...
So for each position in the array, which for loop one is scrolling, for loop two compares the entire array to the single position. Your code should look something like this:
- for(int pos1 = pudding; pos1 < 20; pos1 ++) {
- for(int pos2 = pudding + 1; pos2 < 20; pos2 ++) {
- if(pos1 != pos2) { //if we aren't comparing the same circle
- if(dist(circles[pos1].x, circles[pos1].y, circles[pos2].x, circles[pos2].y) < 10) {
- //this is your code to switch the movement of the circles
- }
- }
- }
- }
Hope this helped...
andrewgies17