ebonydoa
ebonydoa

Reputation: 13

(Newbie Processing Q) How do you slow down the the fading of colors? And how do you stop a moving object?

I'm a newbie who working on processing code. I'm using a variable to fade light blue into a darker blue and that's working fine, but I was wondering how I could slow the process of it fading.

Another question(hope people don't mind me asking two on one post), is how do I make a shape stop moving at a certain point? I have a ellipse there labelled the sun. I'd like it to stop when x=700.

Here's my coding:

float x = 0;
float y = 0;
float r = 0;
int gb = 0;

void setup() {
  size(800, 600);
  background(gb, gb, 255);
  imageMode(CENTER);
  noStroke();
}

void draw() {
  background(0, gb, 255);
  gb++;

  if (gb>50) {
    //the sun
    fill(243, 230, 0);
    ellipse(x, 60, 75, 75);
    fill(243, 230, 0, 80);
    ellipse(x, 60, 90, 90);
    x++;
  }

  fill(0, 255, 0);
  rect(0, 380, 800, 450);
}

Upvotes: 1

Views: 546

Answers (1)

Kevin Workman
Kevin Workman

Reputation: 42174

I was wondering how I could slow the process of it fading.

Check out this line:

gb++;

Here you're incrementing (adding 1 to) your gb variable, which you're using to determine the color. To slow down the color changing, just add a smaller value to it. Something like this:

gb = gb + .1;

Which can be shortened to:

gb += .1;

For this to work, you'll have to change your gb variable to be a float so it can hold decimals.

You might also want to check out the lerp() and map() functions in the reference.

Another question(hope people don't mind me asking two on one post), is how do I make a shape stop moving at a certain point? I have a ellipse there labelled the sun. I'd like it to stop when x=700.

In the future, please only ask one question per post. And try to put together a MCVE for each question instead of posting your whole sketch.

But you can do this with an if statement that only increments x when it is less than 700. Like this:

if(x < 700){
  x++;
}

Shameless self-promotion: I wrote a tutorial on using if statements to create animations in Processing available here.

Upvotes: 0

Related Questions