We are about to switch to a new forum software. Until then we have removed the registration on this forum.
This is what I want.
mousePressed gives me red ellipse
mousePressed gives me blue ellipse
mousePressed gives me white ellipse
mousePressed gives me red ellipse
mousePressed gives me blue ellipse
mousePressed gibes me white ellipse
and so on and so on
I probably need to use for loop but don't know how.
This is what I have now
void setup() {
size(500, 500);
background(0, 255, 0);
}
void draw() {
}
void mousePressed() {
background(0, 255, 0);
fill(255, 0, 0);
ellipse(width/2, height/2, 20, 20);
}
Answers
Hello!
do the display of the ellipse in draw
use a variable of type color (defined before setup)
in mousePressed just change the color via random first
later have a array of the exact colors and use an index for this
Best, Chrisir ;-)
I made first two steps, don't know what do you mean with the third step
" in mousePressed just change the color via random first "
you need to put those colours in an array ideally...
now you can define an index into this array
can select the current colour using this index
and in mousePressed() just increment the index, looping around from 2 to 0
i would recommend not using red or blue as a variable name though - they are method names in processing and whilst this is fine in java version of processing it'll break the javascript version.
Thanks both of you. This is really nice community.
going back far before what koogs said, just the color in a variable and a random color would look like this:
but the requirement is three defined colours, cycling between them. the "random" in the title was misleading
(that said, my first answer (since deleted) was for purely random colour changing - i think the description may've been changed)
Is it possible to short this code
in one line code?
@koogs: correct, but from a didactic point of view the step I took is not a bad one.
The sketch teaches several things:
mousePressed
but in draw...also, I didn't want to write the code for him so I just presented an early step of getting there.
there are 4 things that need doing, there are 4 lines of code...
you might be able to define the array and the values within it but the difference would be measured in characters rather than lines.
actually defining the array thus
means you don't need the three colour[] lines in setup()
Well, kind of:
OR