Emilio
Emilio

Reputation: 25

Constructor does not exist in Processing

In the code below, I was trying to create a simple programme where you just click with you're mouse on the screen to create an other ball that bounces around. I says in the error console that the constructor does not exist on the line where there is the mousePressed function, I'm not sure what the mistake is, can anyone tell me what's wrong with my code?

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

Upvotes: 1

Views: 2768

Answers (1)

Kevin Workman
Kevin Workman

Reputation: 42176

The error says it all: you're trying to call a Ball constructor with two arguments. There is no Ball constructor with two arguments.

You probably want something like this:

class Ball{
   float ballX;
   float ballY;

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

Side note: your code will be a lot easier to read if you use proper indentation (the Processing editor can format your code for you) and naming conventions (variables and functions should start with lower-case letters).

Upvotes: 2

Related Questions