# Controlling drawing speed

**URL:** https://discourse.processing.org/t/controlling-drawing-speed/31929
**Category:** Coding Questions
**Created:** [August 25, 2021, 9:30pm UTC](https://discourse.processing.org/t/controlling-drawing-speed/31929 "2021-08-25T21:30:00Z")
**Posts on this page:** 1
**Showing post:** 2

<div class="post-metadata">

### Author: ![josephh](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/josephh/32/210_2.png) [@josephh](https://discourse.processing.org/u/josephh)
#### Post date: [August 25, 2021, 9:55pm UTC](https://discourse.processing.org/t/controlling-drawing-speed/31929/2 "2021-08-25T21:55:24Z")

</div>

Hi @mews,

Welcome to the forum! 🙂

Let’s suppose you have the following Processing program:

```processing
void drawMissiles() {
  // Loops infinitely
  while (true) {}
}

void setup() {
  size(500, 500);
}

void draw() {
  drawMissiles();

  // This is never reached
  circle(50, 50, 10);
}

```

The execution steps are:

- The `setup()` function gets called at the beginning

- At the first draw loop, the `drawMissiles()` function gets called and does a `while` loop that never finish (true is always true).

- The `circle` function is never called since the draw() function is waiting for the `drawMissiles` function to finish.

This is to explain that if you call any function inside the `draw()` loop, it will wait until the function returns a value or finishes.

So in your code, you are increasing the position and drawing a point in the while loop but you see it flash on the screen because it does the while loop at each draw loop which is not what you want.

The draw loop is actually the main loop of your program so you need to use it in order to animate objects at each frame (since it’s called roughly 60 times per second). The update code is then called once every loop.

Check this other program:

```processing
PVector ballLocation;

void setup() {
  size(500, 500);
  
  ballLocation = new PVector(0, height / 2);
}

void draw() {
  background(255);
  
  // Move the ball
  ballLocation.x += 5;
  
  // Make it wrap when it goes out of screen
  if (ballLocation.x > width) ballLocation.x = 0;
  
  // Draw the ball
  fill(255, 0, 0);
  circle(ballLocation.x, ballLocation.y, 100);
}

```

![ball_anim](https://canada1.discourse-cdn.com/flex036/uploads/processingfoundation1/original/2X/d/d33b0e2eeff709485921ac51dfe5bb747028600b.gif)

So rather than making a while loop where I update the location of my ball, the draw loop does the job for me 😉

One more thing is that you should use Object Oriented Programming which makes program structure more easy to work with. You can check an old post I wrote on this subject:

> [@Problems with my mini game](https://discourse.processing.org/t/problems-with-my-mini-game/21949/2):
>
> Hi, Welcome to the community! slight_smile Before trying to solve your problem, you are facing a well known problem where the structure of your code is actually describing [object oriented programming](https://en.wikipedia.org/wiki/Object-oriented_programming) : So object oriented programming is a way to structure your code by using objects to model the data that your are manipulating. In this special case, your are creating Balls, and a Ball is described as : a position (x and y coordinates on the screen) a speed (described as a vector, xDir and y…

---

_[View the full topic](https://discourse.processing.org/t/controlling-drawing-speed/31929)._
