# Trying to eliminate draw flicker

I had this working reasonably well several months ago but I must have forgotten to save the project. I had the moon orbiting the earth and the earth orbiting the sun with minimal flicker. I’ve read through many posts but cannot relate the answers to my project. What have I forgotten?

// The PShape object
PShape earth;

float orbitx, orbity;

float angle = 0.0;
int i = 0;
int timeEnd;

void setup()
{
size(600, 600, P2D);

// Create the earth as a PShape
earth = createShape(ELLIPSE, 0, 0, 50, 50);
earth.setStroke(color(0, 0, 255));
earth.setFill(color(0, 0, 255));

timeEnd = millis();
}

void draw()
{
background(204);
if (millis() > timeEnd + 100)
{
timeEnd = millis();

``````i += 1;
if (i > 90)
{
i = 0;
}
angle = (i * TAU) / 90;
orbitx = (sin(angle) * radius) + width/2;
orbity = (cos(angle) * radius) + height/2;

translate(orbitx, orbity);
shape(earth);
``````

}
}

I haven’t withdrawn this post, or if I did I didn’t mean to.

I discovered that I have background() function in the wrong place, so no more flickering. However, there’s probably a better way of doing this.

Haha, that doesn’t mean you withdrew your post, but that I withdrew mine, which I did because I thought I had misunderstood your question.

If you want to get rid of the flickering, you could either do that, or put the code for drawing outside of the if-statement (so `translate` and `shape`). Both of those options won’t be smooth though, so if you want that, you could just get rid of that condition all together.

Well you have the `if (millis() > timeEnd + 100)` in there, drawing every 100 ms (10 FPS), so that might have something to do with it?

Apparently you solved it in a different way (changing where background is), so it could be interesting to see how you did it.

Btw, there’s also a frameRate() function.

I am not sure I understand what you desire.
If you want to eliminate the flicker, why not draw every single frame -> remove the mills() stuff.

If you want the flicker but want to control it, I usually use modulo.
->
if (mills()%500 == 0)) {
do something every 500 ms
}

so you could control how often the earth is drawn…

Thank you Schred, raron and Hyperion65 for your replies.

I thought that I had to set up a timed event to trigger the Draw() function in the same way as other languages but it seems that I don’t. I’ll experiment further.
By the way, I found my original code, silly me saved it under some obscure name that I’d forgotten.

I just confirmed that all I need to do is move the translate and shape functions outside of the if loop, as suggested. Thanks again.

1 Like