I am trying to change the color of a PShape when I press a key, as a marker that that key is pressed. Is there a way that I can do this without redefining the PShape using beginShape()
and endShape()
with a new fill every time?
Here is an example of what I am attempting to do:
PShape s;
void setup() {
size(100,100);
s = createShape(RECT,25,25,50,50);
s.setFill(color(75,183,19));
shape(s);
}
void draw() {
if(keyPressed == true) {
if(key == 'w') {
s.setFill(color(217,93,47));
}
}
}
the setFill()
function works before I actually put the shape into the window, but it won't in the draw()
function. I have also tried using fill()
.
I have also tried using disableStyle()
when declaring the shape and changing the fill before redrawing the shape:
PShape s;
void setup() {
size(100,100);
shape();
fill(75,183,19);
shape(s);
}
void draw() {
if(keyPressed == true) {
if(key == 'w') {
fill(217,93,47);
shape();
}
}
}
void shape() {
s = createShape(RECT,25,25,50,50);
s.disableStyle();
}
This also doesn't work, the shape just stays as the first color. Is there any function I am missing which will help me to achieve this?
The following code will change the color of a PShape square each time a designated key is pressed:
PShape s;
color chosenColor;
void setup() {
size(100,100);
chosenColor = color(random(255), random(255), random(255));
}
void draw() {
myShape();
}
void myShape() {
s = createShape(RECT,25,25,50,50);
s.setFill(chosenColor);
shape(s);
}
void keyPressed() {
if (key == 'w') {
chosenColor = color(random(255), random(255), random(255));
}
}