(新手处理Q)如何减缓颜色的褪色?你如何停止移动的物体?

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

我是一名处理代码的新手。我正在使用一个变量将浅蓝色淡化为深蓝色,效果很好,但我想知道如何才能减缓它的淡化过程。

另一个问题(希望人们不介意我问二对一 post),我如何让一个形状在某个点停止移动?我有一个标有太阳的椭圆。我希望它在 x=700 时停止。

这是我的编码:

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

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

查看这一行:

gb++;

此处您正在递增(加 1)您的 gb 变量,您使用它来确定颜色。要减慢颜色变化,只需为其添加一个较小的值。像这样:

gb = gb + .1;

可以缩短为:

gb += .1;

为此,您必须将 gb 变量更改为 float 以便它可以保存小数。

您可能还想查看 the reference 中的 lerp()map() 函数。

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.

以后每个post只问一个问题。并尝试为每个问题拼凑 MCVE 而不是 post 整个草图。

但是您可以使用 if 语句来做到这一点,该语句仅在小于 700 时递增 x。像这样:

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

无耻的自我推销:我在 Processing available here.

中写了一篇关于使用 if 语句创建动画的教程