You're almost there. The problem is that while you have correctly declared the instance variables
y
,
x
,
radius etc. in the line immediately below
class Ball() {, you then declare a separate set of the variables with the same names in the
Ball
constructor. As far as Processing is concerned, this new set used by the constructor is completely independent of the instance variables that are used by display(), so their initialisation has no effect once the constructor has completed. The solution is to initialise these values in the constructor but not declare them, by removing the
float
, and
int
declarations:
Ball bounce1;
void setup()
{
size(200, 200);
fill(100, 204);
bounce1 = new Ball();
frameRate(30);
colorMode (RGB);
}
void draw()
{
background(0);
bounce1.display();
}
class Ball {
float y, x, radius, speedX, speedY, directionX, directionY;
int ballColor;
Ball() {
y = 30.0;
x = 30.0;
radius = 20.0;
speedX = 1.0;
speedY = 1.0;
directionX = 1;
directionY = 1;
ballColor = 150;
}
void display() {
fill(204, 102, 0);
ellipse(x, y, radius, radius);
ellipseMode(CENTER);
stroke(5);
}
}