Processing 处理|如何阻止球改变';s颜色

Processing 处理|如何阻止球改变';s颜色,processing,Processing,下面是我编写的一个小代码,可以让球在处理过程中反弹。球应该改变它的颜色,所有它从“地面”反弹回来的东西,变得越来越慢,最后停在地面上。 但是-这就是我遇到的问题-球在底部没有停止改变它的颜色-这意味着它没有停止反弹,对吗 问题是:我如何告诉球停下来,不再改变它的颜色 float y = 0.0; float speed = 0; float efficiency = 0.9; float gravitation = 1.3; void setup(){ size(400, 700)

下面是我编写的一个小代码,可以让球在处理过程中反弹。球应该改变它的颜色,所有它从“地面”反弹回来的东西,变得越来越慢,最后停在地面上。 但是-这就是我遇到的问题-球在底部没有停止改变它的颜色-这意味着它没有停止反弹,对吗

问题是:我如何告诉球停下来,不再改变它的颜色

float y = 0.0;
float speed = 0;
float efficiency = 0.9;
float gravitation = 1.3;


void setup(){
  
  size(400, 700);
  //makes everything smoother
  frameRate(60);
  //color of the ball at the beginning
  fill(255);
  
}

void draw() {
  
  // declare background here to get rid of thousands of copies of the ball
  background(0);
  
  //set speed of the ball
  speed = speed + gravitation;
  y = y + speed;
  
  //bouce off the edges
  if (y > (height-25)){
    //reverse the speed
    speed = speed * (-1 * efficiency);
    
    //change the color everytime it bounces off the ground
    fill(random(255), random(255), random(255));
  }
  
  //rescue ball from the ground
  if (y >= (height-25)){
    y = (height-25);
  }
 
/*
  // stop ball on the ground when veloctiy is super low
  if(speed < 0.1){
    speed = -0.1;
  }
*/
  
  // draw the ball
  stroke(0);
  ellipse(200, y, 50, 50);
  
}
float y=0.0;
浮动速度=0;
浮子效率=0.9;
浮子引力=1.3;
无效设置(){
大小(400700);
//让一切变得更顺利
帧率(60);
//开始时球的颜色
填充(255);
}
作废提款(){
//在这里声明背景以消除数千份球的副本
背景(0);
//设定球的速度
速度=速度+重力;
y=y+速度;
//边上的花束
如果(y>(高度-25)){
//倒车
速度=速度*(-1*效率);
//每次它从地面反弹时都要更改颜色
填充(随机(255)、随机(255)、随机(255));
}
//从地上救球
如果(y>=(高度-25)){
y=(高度-25);
}
/*
//当速度超低时,将球停在地面上
如果(速度<0.1){
速度=-0.1;
}
*/
//抽签
冲程(0);
椭圆(200,y,50,50);
}

问题是,即使在较小时将速度设置为
-0.1
,并且将
y
设置为
height-25
,下一次循环将
重力
添加到
速度
然后将
速度
设置为
y
,使
y
大于
height-25
(略多于1个像素)再次。这使球在零高度的无限跳跃循环中上下跳跃

您可以对反射速度使用阈值。如果低于阈值,则停止循环

在文件顶部,添加一行,如

float threshold = 0.5; //experiment with this
然后在
draw()
中,就在该行之后

speed = speed * (-1 * efficiency);
添加行

if(abs(speed) < threshold) noLoop();
if(abs(速度)

在这种情况下,您可以扔掉检查速度是否超低的
if
子句。

非常感谢您的帮助!1.我不太明白noLoop()在做什么。2.abs(速度)意味着什么?检查int值(而不是float值)绝对是解决方案。