Hi, I’m currently making a “sandwich creator” minigame, where the programs start with an intro of one slice of bread dropping down, and the user can click to put shapes on it as “toppings”. The issue I’m running into is whether to place background() in set() or draw(). If I place it in draw(), every time it refreshes it erases the previously created shapes, if I place it in set(), the initial movement of the bread dropping down will not refresh at all and every frame of its movement persists on the screen.
Is there a way to let the initial movement play with an updating background but stop having the background update once the interaction starts?
PS: The randomly generated shapes also seem to be lagging, if clicking is spammed the same exact shape will show up multiple times in a row, is there a way to fix the lag?
// Creating variables
float x_position; // x position of the bread
float y_position; // y position of the bread
float speed; // Speed
float gravity; //Gravity
PImage slice; //Image of bread
float rect_high;
float rect_width;
float xPos = 0;
float yPos = 0;
int high, wide, clr;
color[] c = {
#fffaeb, #eb4034, #60a35f, #f4f788, #82604b //Color scheme
};
boolean is_clicked = false;
// Initial set up
void setup() {
slice = loadImage("slice.png");
size(1000, 1000);
background(0);
x_position = 250;
y_position = 0;
speed = 0;
gravity = 0.3;
strokeWeight(0);
}
void draw() {
//Add bg and bread
background(c[0]);
image(slice, x_position, y_position);
// Using gravity to make things fall
y_position = y_position + speed;
speed += gravity;
// Stopping Bread
if (y_position > height-400)
speed = 0;
if(is_clicked==true) {
fill(c[clr]);
noStroke();
ellipse(xPos, yPos, high, wide);
}
}
void mouseClicked()
{
is_clicked = true;
clr = (int)random(1,4);
high = (int)random(100,255);
wide = (int)random(100,255);
xPos = mouseX;
yPos = mouseY;
}