Issue with deciding to place background in draw() or set()

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;
}

1 Like

basically both way using
background(bg);
have their use.
this are not correct terms but i call it
in setup()

painting mode

in draw()

drawing mode

so when you moved to drawing mode you see that you have to
do / draw all again ( actually 60 times per sec )
what you need to show,
and that is the most used way with processing.
but (re)drawing a paint brush trail with it would be a nightmare.

i am not fully sure, but on the first look i would say
for your “job” the drawing way is best.

now your code uses a image we not have so it is difficult to test,
i replaced it with a temporary rectangle and see it fall.
so there is actually no problem with this in the code…

shapes i not see here??
mouse action i not see here??

1 Like