Hello guys,
I’m new to processing and have been recently following some youtube online tutorials to learn.
The topic is called ArrayList. However, when I followed the scripts some errors occurred, suggesting"The function “add(Ball) does not exist”. It works in the tutorial but not in my case and I don’t know why.
Here is the script:
ArrayList ballCollection;
void setup(){
size(600,600);
smooth();
ballCollection = new ArrayList();
}
void draw(){
background(0);
Ball myBall = new Ball(random(0,200),random(0,200));
ballCollection.add(myBall);
for(int i=0;i<ballCollection.size();i++)
{
Ball myB = (Ball) ballCollection.get(i);
myB.run();
}
}
class Ball{
//global variables
float x = 0;
float y = 0;
float speedX = 4;
float speedY = 0.5;
//constructor
Ball(float _x, float _y)
{
x = _x;
y = _y;
}
//fuctions
void display(){
ellipse(x,y,20,20);
}
void move(){
x +=speedX;
y +=speedY;
}
void bounce(){
if(x>width){
speedX *=-1;
}
if(x<0){
speedX *=-1;
}
if(y>width){
speedY *=-1;
}
if(y<0){
speedY *=-1;
}
}
void gravity(){
speedY+=0.2;
}
void run(){
display();
move();
bounce();
gravity();
}
}
Thank you, guys!