Hi, I’m in a class where we adapt code to fulfill the assignments. I’m working on mouse interaction with the redraw() function in Processing. What I’m trying to do is have the leaf colours change everytime the mouse is pressed. How do I get the leaf colour to change and where do I write this in? Eventually I would like the leaves to fall but I’m stuck. Any tips are appreciated. The class lesson involves functions and we haven’t gotten to classes yet…
/*
Task: Develop a program that incorporates more than one function. Once this has
been completed, try to vary or disrupt the pattern dynamically using mouse interaction.
*/
//VARIABLES
color c = color(189, 240, 201); //leaf color
color d = color(171, 211, 158);//leaf color after mouse pressed?
//SETUP
void setup()
{
size(400,600);
background(16, 106, 37);
smooth();
noStroke();
noLoop();
}
//DRAW
void draw()
{
fill(c);
vine(40, 9, 16);
vine(80, 9, 16);
vine(120, 9, 16);
vine(160, 9, 16);
vine(200, 9, 16);
vine(240, 9, 16);
vine(280, 9, 16);
vine(320, 9, 16);
vine(360, 9, 16);
}
//New vine function
void vine(int x, int numLeaves, int leafSize)
{
stroke(134, 100, 95);
line(x, 0, x, height);
noStroke();
int gap = height / numLeaves;
int direction = 1;
for (int i = 0; i < numLeaves; i++)
{
int r = int(random(gap));
leaf(x, gap*i + r, leafSize, direction);
direction = -direction;
}
}
//Previous leaf function
void leaf(int x, int y, int size, int dir)
{
pushMatrix();
translate(x,y);
scale(size);
beginShape();
vertex(1.0*dir, -0.7);
bezierVertex(1.0*dir, -0.7, 0.4*dir, -1.0, 0.0, 0.0);
bezierVertex(0.0, 0.0, 1.0*dir, 0.4, 1.0*dir, -0.7);
endShape();
popMatrix();
}
void mousePressed()
{
redraw(); // Run the code in draw one time
fill (d);//Leaf colour changes each time mouse is pressed?
}
type or paste code here