# Movement of shapes in an orbit

**URL:** <https://discourse.processing.org/t/movement-of-shapes-in-an-orbit/2688>\
**Category:** Coding Questions\
**Created:** [August 16, 2018, 8:20am UTC](https://discourse.processing.org/t/movement-of-shapes-in-an-orbit/2688 "2018-08-16T08:20:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kirrat](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@Kirrat](https://discourse.processing.org/u/Kirrat)\
**Post date:** [August 16, 2018, 8:20am UTC](https://discourse.processing.org/t/movement-of-shapes-in-an-orbit/2688/1 "2018-08-16T08:20:51Z")

</div>

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

```auto
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);
}

}

```

---

<div class="post-metadata">

**Author:** ![tony](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tony/32/949_2.png) [@tony](https://discourse.processing.org/u/tony)\
**Post date:** [August 16, 2018, 9:12am UTC](https://discourse.processing.org/t/movement-of-shapes-in-an-orbit/2688/2 "2018-08-16T09:12:23Z")

</div>

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:

> [@Kirrat](#):
>
> 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.

What exactly does [mousePressed](https://processing.org/reference/mousePressed.html) 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?

---

<div class="post-metadata">

**Author:** ![TfGuy44](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/tfguy44/32/41_2.png) [@TfGuy44](https://discourse.processing.org/u/TfGuy44)\
**Post date:** [August 16, 2018, 9:14am UTC](https://discourse.processing.org/t/movement-of-shapes-in-an-orbit/2688/3 "2018-08-16T09:14:56Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![Kirrat](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@Kirrat](https://discourse.processing.org/u/Kirrat)\
**Post date:** [August 16, 2018, 10:44am UTC](https://discourse.processing.org/t/movement-of-shapes-in-an-orbit/2688/4 "2018-08-16T10:44:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Kirrat](https://avatars.discourse-cdn.com/v4/letter/k/d78d45/32.png) [@Kirrat](https://discourse.processing.org/u/Kirrat)\
**Post date:** [August 16, 2018, 10:47am UTC](https://discourse.processing.org/t/movement-of-shapes-in-an-orbit/2688/5 "2018-08-16T10:47:32Z")

</div>

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