Processing中不存在构造函数

时间:2017-05-12 18:52:16

标签: processing

在下面的代码中,我试图创建一个简单的程序,您只需在屏幕上点击鼠标即可创建另一个弹跳的球。我在错误控制台中说,构造函数不存在于有mousePressed函数的行上,我不知道错误是什么,有人能告诉我代码有什么问题吗?

Ball ball;
ArrayList<Ball> balls = new ArrayList<Ball>();
void setup() {
size (640, 360);
ball = new Ball();
ball.Setup();
}

void draw () {
background (55); 
ball.show();
ball.update();
}

void mousePressed() {

balls.add(new Ball(mouseX, mouseY));
}

class Ball{

float a;
float b;
float movex;
float speedx;
float movey;
float speedy;
int fcolor;

void Setup(){
fcolor = 255;
a = random (-6, 6);
speedx = width/2;
b = random (6, -6);
speedy= height/2;
if (a < 0) {
movex = -6;
} else {
movex = 6;
}
if (b < 0) {
movey = -6;
} else {
movey = 6;
}
}

void show(){

fill (fcolor);
stroke (fcolor);
ellipse (speedx, speedy, 50, 50);

}
void update() {

speedx = speedx + movex;
speedy = speedy + movey;

if (speedx > width) {
speedx = width;
movex = -movex;
fcolor = color(random(255),random(2,55),random(0,255));
}

if (speedx < 0) {
speedx = 0;
movex = -movex;
fcolor = color(random(0,255),random(0,255),random(0,255));
speedy = speedy + 0.2;
}

if (speedy > height) {
speedy = height;
movey = -movey;
fcolor = color(random(0,255),random(0,55),random(0,255));
}
if (speedy < 0) {
speedy = 0;
movey = -movey;
fcolor = color(random(0,255),random(0,255),random(0,255));
 }
 }
}

1 个答案:

答案 0 :(得分:1)

错误说明了一切:您正在尝试使用两个参数调用Ball构造函数。没有Ball构造函数有两个参数。

你可能想要这样的东西:

class Ball{
   float ballX;
   float ballY;

   public Ball(float ballX, float ballY){
      this.ballX = ballX;
      this.ballY = ballY;
   }
}

附注:请确保在发布代码时使用适当的缩进(处理编辑器可以为您格式化代码)和命名约定(变量和函数应以小写字母开头)。