我想知道为什么功能mousePressed
有延迟。我知道我的代码有问题特别是在for循环中,如果有两个球在一起,如果我按下它们,下面的那个球有时会先消失。有时候上面那个先消失了
int number=80;
int i;
ball [] balls =new ball[number];
void setup(){
size(1000,1000);
frameRate(60);
for(i=0 ;i<number;i++)
{
balls[i]=new ball(color(random(0,255),random(0,255),random(0,255)),random(30,970) , random(30,970),random(1.9,2));
}
}
void draw()
{
background(255,0,0);
for( i=number-1 ;i>=0;i--)
{
if (mousePressed == true) {
balls[i].disapeear();
}
balls[i].display();
balls[i].bouncing();
}
}
class ball
{
float speed,x,y;
color c;
float A=1;
float B =1;
ball(color colour ,float horiz,float vert,float s)
{
speed = s;
x = horiz;
y = vert;
c = colour;
}
void disapeear()
{
float L = sqrt((x-mouseX )*(x-mouseX))+((y-mouseY)*(y-mouseY));
if(L<15)
{
x=-100;
y=-100;
}
}
void bouncing()
{
x=x+(speed * A);
y=y+(speed * B);
if((x>width-30)||(x<0))
{
A =A * -1 ;
}
if((y>height-30)||(y<0))
{
B =B * -1 ;
}
}
void display()
{
fill(c);
stroke(0,0,255);
ellipse(x,y,30,30);
}
}
当我看你的代码时,你没有的一件事是mousePressed()
函数。
如果你把这个函数添加到主类中,可能会停止延迟。