We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I want to change the ellipse to a rectangle once the ellipse reaches the width of the canvas. Then turn it back to an ellipse once the rectangle is less then than canvas.
float circleX = 0;
float speed = 1;
color bg_color = color(0);
void setup() {
size(640, 360);
}
void draw() {
circleX = circleX + speed;
if (circleX > width) {
speed--;
bg_color = color(255, 0, 0);
fill(0, 0, 255);
rect(180, 180, 24, 24);
}
if (circleX < 0) {
speed++;
bg_color = color(0, 250, 0);
}
background(bg_color);
fill(255);
ellipse(circleX, 180, 24, 24);
}
Answers
This is the same question as the other one.
Add a new variable, a boolean, that remembers if you are drawing a rectangle or an ellipse.
Update its value in the correct places.
Use its value when drawing.
Nice - Thanks MAN!!!
What if I wanted to add a third shape to this code?
Then the variable a needs to be replaced by an int to be able to hold more than two values. For instance,
Then you manage the shape to draw like this
Kf
OK I am lost. Are you saying that all the values are held in a variable called "final?"
https://Processing.org/reference/final.html
So "currentShape" is the variable? Is it an array?
Variable currentShape holds a value which represents which shape to draw. :-B
TRIANGLE
Check this modified version of your code.
Kf
This works well when you have a few states and they are simple.
There is another approach to this for when you have many states or they are complex.
Say that your shape might be a circle, square, or triangle, and it might be filled red, blue, or green, and it might be big or little, and it might have thick or thin lines (depending on program state), and the lines might be black or white.
draw()
, justimage(pg, circleX, 180)
renderShape()
renderShape()
, and draw it to the PGraphics pg. Now you aren't checking 32 nested conditions every frame -- you just draw a different shape whenever something changes....then, if there are many shapes, make it a class and make
renderShape()
a method of that class.thanks Kf, loop & jeremy
Hello Kf,
I am still looking at the code you posted. Some of it I get some goes over my head. Such as, how did you get %3? currentShape=currentShape%3 why put a % at all? I tried a couple different numbers 3 gives the best results.
1rulesupersedes
Check: https://processing.org/reference/modulo.html
It is one way to reset the variable when it goes over a counter. Consider:
Every time there is a mouse event, currentShape is increased by one. You need to make sure that currentShape takes only valid values. Since there are three figures labeled from 0 to 2, then we have to check we don't overflow. The modulo op is one way. Another standard way is
I hope this helps,
Kf