Help needed. We need to make an algorithm where:
- if you click somewhere with left mouse button, a rectangle appears (so if you click again with left mouse button, another one will appear and the old one will remain)
- if you drag your mouse, that same rectangle that appeared with left mouse button click will change colour depending on where your mouse is (but not the other rectangles, only the one you just spawned with left click)
- if you click with your right mouse button, everything dissapears (i just let another bigger rectangle spawn the same colour as the background)
The problem is, whenever i drag with my mouse, yes the colour of the rectangle does change, but it just wont stop making rectangles and we have to make only 1 rectangle at the position where you clicked your mouse. Can someone help??? (I am still a beginner, so no complex code pls).
Here is a photo of the example assignment: as you can see, the rectangles are different colours. When you click somewhere, a rectangle will appear. If you drag, the rectangle will turn for example pink. But if you click again somewhere else, another rectangle will appear in the same colour as the last one (except if you drag your mouse, then the colour will change).
Here is my extremely nooby code, someone pls help me fix it <3
</>
void setup()
{
size(600, 600);
}
float r;
float g;
void mouseDragged()
{
float rood = mouseX;
float groen = mouseY;
float r = map(rood, 0, width, 0, 255);
float g = map(groen, 0, height, 0, 255);
}
void draw()
{
if(mousePressed == true && mouseButton == LEFT)
{
fill(r, 0, g);
rectMode(CENTER);
rect(pmouseX, pmouseY, 100, 100);
}
if(mousePressed == true && mouseButton == RIGHT)
{
fill(200);
rect(200, 200,800, 800);
}
}
</>