Movement of shapes in an orbit

Hi
I’m trying to make a shape appear and start moving in an orbit whenever any of the shape above is clicked. So far i’m able to make shapes appear but i want a single shape to appear on a single click but as you can see after running the code that the shapes are appearing in groups. More over i want the previous shape to remain visible and orbiting when the next shape is clicked. Secondly please guide me on how to control the orbiting speed.
Here’s the code

type orbitDuration = 5*random(500,1500); 
float orbitRad = 70;
float x,y;
int n=0,i=0,j=0,k=0;
void setup()
{
  
  size(630,360);
  background(251,255,71,0.97);
   
}
void draw()
{

   background(251,255,71,0.97);
  //making ellipse without border
  noStroke();
  fill(125);
  ellipse(50, 44, 52, 52);
  //making rectangle without border
  noStroke();
 fill(125);
  rect(150,20,50,50);
  noStroke();
  fill(125);
  triangle(300,20,260,65,340,65);
  stroke(125);
  strokeWeight(13);
  line(400,20,440,55);
  
 
  if ((mousePressed)&&(mouseX > 0) && (mouseX < 100)){
 n++;
}
   else if ((mousePressed)&&(mouseX > 120) && (mouseX < 200))
  {
    i++;
  }
else if((mousePressed)&&(mouseX>259)&&(mouseX<341))
{
  j++;
}
else if((mousePressed)&&(mouseX>399)&&(mouseX<441))
{
  k++;
}
  for (int p=0;p< n ; p++)
  {   
    
  
       float ang = TWO_PI+(random(1,5)) * millis()/orbitDuration;
       float x = cos(ang)*orbitRad;
       float y = sin(ang)*orbitRad;
       noStroke();
       fill(148,148,142,255);
       ellipse(x+300, y+200,15, 15);  
       
  }
  for (int q=0; q<i; q++){
     float ang = TWO_PI+(random(1,5)) * millis()/orbitDuration;
       float x = cos(ang)*orbitRad;
       float y = sin(ang)*orbitRad;
       noStroke();
       fill(20);
    rect(x+300,y+200,10,10);
    
  }
   for (int t=0; t<j; t++){
     float ang = TWO_PI+(random(1,5)) * millis()/orbitDuration;
       float x = cos(ang)*orbitRad;
       float y = sin(ang)*orbitRad;
       noStroke();
       fill(125);
       triangle(x+300,y+200,x+295,y+208,x+305,y+208);
}
for (int l=0; l<k; l++){
     float ang = TWO_PI+(random(1,5)) * millis()/orbitDuration;
       float x = cos(ang)*orbitRad;
       float y = sin(ang)*orbitRad;
       stroke(50);
       strokeWeight(3);
      line(x+300,y+200,x+310,y+204);
}

}

An important skill to learn in programming (or any problem solving for that matter) is the ability to break down a problem into smaller, easier to solve pieces.

How about we focus on just that first bug you have:

What exactly does mousePressed tell us? If draw() is called 24 times a second, and your if-statements run 24 times a second, what happens if you hold that mouse-button down for only half a second?

1 Like

Then once you have that worked out, look in the Reference at the mousePressed() function. It’s not the boolan you have been using! It’s a different, top-level function that goes at the same level as draw() and setup(). How many times does that function run? When? Might that be useful?

1 Like

by mousePressed i wanted to make sure the shape appears only when the mouse is clicked. I tried creating a separate event for mouseClicked but then the shape would not keep orbiting since the code was no longer within draw

Yes i have tried to create a separate function for mousePressed as well but then the shape only appears when the mouse is pressed