Can someone please clarify why the rect() function is not being called?
I have checked it and code in the if function is being executed as normal, just functions to do with drawing are not working.
public class FadeCubes{
IntList brightval = new IntList();
IntList x = new IntList();
IntList y = new IntList();
FadeCubes(){}
void update(){
for(int i = 0; i < brightval.size(); i++){
brightval.sub(i,17);
if(brightval.get(i) == 0){
brightval.remove(i);
x.remove(i);
y.remove(i);
}else{
fill(brightval.get(i));
rect(x.get(i),y.get(i),20,20);
}
}
println(x);
}
void addcube(int inx, int iny){
brightval.append(255);
x.append(inx);
y.append(iny);
}
}
I’ve added more code so I could test it here and the cubes got drawn for me.
However when you have a loop which got remove() inside it you should make it backwards instead!
This is a slightly modified version of your code which includes the backwards loop:
// https://Discourse.Processing.org/t/rect-function-not-working-
// when-used-inside-of-a-function-inside-of-a-class/34347/2
// 2021-Dec-29
static final int INIT_CUBES = 10;
final FadeCubes cubes = new FadeCubes();
void setup() {
size(400, 300);
frameRate(1);
for (int i = 0; i < INIT_CUBES; ++i) mousePressed();
}
void draw() {
background(0);
cubes.update();
println(frameCount, cubes);
}
void mousePressed() {
final int
x = (int) random(width - FadeCubes.SIZE),
y = (int) random(height - FadeCubes.SIZE);
cubes.addCube(x, y);
}
class FadeCubes {
static final int INIT_BRIGHT = 255, BRIGHT_LOSS = 17, SIZE = 20;
final IntList brights = new IntList();
final ArrayList<PVector> positions = new ArrayList<PVector>();
void addCube(final int x, final int y) {
brights.append(INIT_BRIGHT);
positions.add(new PVector(x, y));
}
void update() {
for (int i = brights.size(); --i >= 0; ) {
brights.sub(i, BRIGHT_LOSS);
final int gray = brights.get(i);
if (gray > 0) {
final PVector pos = positions.get(i);
fill(gray);
square(pos.x, pos.y, SIZE);
} else {
brights.remove(i);
positions.remove(i);
}
}
}
String toString() {
return "Cubes: " + brights.size();
}
}