I am have tried and somewhat suceeded in creating a function which creates a fade from black to white after a random amount of time and then resets itself.
i am trying to fill the screen with a grid of these triangles so they fade in at random times, i managed to do it by creating a new function for each triangle, after realising that this would result in thousands of lines of code i have used an array and a for loop to create a grid of triangles. Now i am stuck. for some reason which i do not understand it simply selects all of the triangles and fades them in at the same time. Not what i want to happen!
Triangles[] tri = new Triangles[18];
int savedTime;
float totalTime = 5000;
float black = 0;
void setup()
{
size(900, 900);
background(0);
savedTime = millis();
for(int i = 0; i < tri.length; i++){
float y = 0;
float x = 0;
tri[i] = new Triangles(x, y);
}
}
void draw()
{
change();
for(int i = 0; i < tri.length; i++){
tri[i].display();
}
}
class Triangles {
float x;
float y;
Triangles(float tempX, float tempY){
x = tempX;
y = tempY;
}
void display()
{
for(float x = 25; x < width; x+=50){
for(float y = 0; y < height; y+=50){
triangle(x, y, x+25, y+50, x-25, y+50);
}
}
}
}
void change(){
int passedTime = millis() - savedTime;
totalTime = random(0, 15000);
if(passedTime > totalTime){
noStroke();
fill(black);
black += 1;
if(black == 255){
black = 0;
savedTime = millis();
}
}
}
can anyone help me as i feel hopelessly stuck and the answer is just not presenting itself really.
when i try the following in the draw loop i get an error saying “The function change() does not exist.”
again i do not understand why this is.
void draw()
{
for(int i = 0; i < tri.length; i++){
tri[i].change();
tri[i].display();
}
}
if anyone can explain where i am fundamentally going wrong or why this error is happening it would be greatly appreciated!
okay, its probably best i try to figure this out on my own but can you give any pointers as to how i can get each triangle to fade in at a random time using the change function? They are all fading in at the same time.
Now, I moved the variables like int savedTime = millis();
already in the class because time shall be different for each triangle.
What to do
Now the class represents one triangle. ONE.
So display() should be brief: triangle(x, y, x+25, y+50, x-25, y+50);
(the for loop already is in draw() so NOT needed here )
Also you forgot to pass different values for x,y in setup() (because the false for-loop in display() took care of this). So in setup() calculate some values based on i.
Then use background(220); at start of draw() (NOT background(0); because black on black won’t work, you know… )
Now, go and fix your timer. 15 seconds is quite long though.
Okay i’ve moved the for loop into draw, now just one triangle appears. Obviously not working as intended. Should the for loop for the X / Y be in setup? Should i be using variables within the Triangles class for the timer too? These are the only things that come to mind but i don’t want to be flogging a dead horse.
If you could point me towards any other resources i would be very appreciative, again thankyou for your time and apologies on my slowness.
int savedTime;
float totalTime = 5000;
float black = 0;
Triangles[] tri = new Triangles[18];
void setup() {
size(900, 900);
noStroke();
savedTime = millis();
background(0);
tri = new Triangles[18];
// int index = 0;
for (int i = 0; i < 18; i++) {
float x =25;
float y =0;
tri[i] = new Triangles(x, y);
println(i);
}
}
void draw() {
for (int y = 0; y < height; y+=50) {
for (int x = 25; x < width; x+=50) {
for (int i = 0; i < 18; i++) {
tri[i].display();
}
}
}
}
class Triangles
{
float x;
float y;
Triangles(float xpos, float ypos)
{
x = xpos;
y = ypos;
}
void display() {
color black = 0;
noStroke();
int passedTime = millis() - savedTime;
totalTime = random(0, 2500);
if (passedTime > totalTime) {
// fill(black);
fill(255);
triangle(x, y, x+25, y+50, x-25, y+50);
black += 1;
if (black == 255) {
black = 0;
savedTime = millis();
}
}
}
}