Objects

Back to the circle animation:

int myDiameter = 10;
float posX = 250;
float posY = 250;
 
void setup() 
{
  size(500, 500); 
  smooth();
  noStroke();  
}
 
void draw() 
{ 
  background( 0, 0, 0);
  ellipse(posX, posY, myDiameter, myDiameter);
  posX++;
}

If we want to animate a circle we need to save coordinates.
But what if we want to move many circles, each one independently?
We can make more variables and arrays but when we have a whole lot of different entities with different characteristics it would become a nightmare.

The solution to this an many other problems are objects
and Object Oriented Programming OOP

How to write a class

A class is the code to defines a certain kind of object.
Class is the abstract idea, the blueprint. Objects are “instances” of a class.
Think about the idea of a cat and a cat specimen.

The code can go anywhere outside setup and draw or on another file:

//class name capitalized
class Ball
{
  //we move the variables here because
  //they belong only to this class
  //they are "properties" of this class
  int myDiameter = 10;
  float posX = 250;
  float posY = 250;
 
  //we move here all the functions that pertain to the class
  //they are called "methods"
  void update()
  {
  ellipse(posX, posY, myDiameter, myDiameter);
  posX++;
  }
}

Using Objects

Objects need to be declared like variables and created with the command “new”.

//declare an object myBall of the class Ball
Ball myBall;
 
void setup() 
{
  size(500, 500); 
  smooth();
  noStroke();
 
  //create the ball
  myBall = new Ball();
 
  //this is how I access the property of the object I just 
  //created, the default value of posX is immediately overwritten 
  myBall.posX = 100;
 
}
 
void draw() 
{ 
  //bg is not conceptually part of the class so it's in the draw function
  background( 0, 0, 0);
 
  //this is how I call the method update
  myBall.update();
}
 
 
class Ball
{
  //properties with default value
  int myDiameter = 10;
  float posX = 250;
  float posY = 250;
 
  //method that draws and moves the ball
  void update()
  {
  ellipse(posX, posY, myDiameter, myDiameter);
  posX++;
  }
}

Exercises:
Add a color property
Make the ball wrap around the screen
Create another instance of the ball

Arrays of objects

Objects are particularly useful when used in combination with arrays.
This program manages n balls “stored” in a global array.

 
int NUM_BALLS = 10;
 
// creating an (empty) global array with a
// capacity of 10 elements of type Ball
Ball[] myBalls = new Ball[NUM_BALLS];
 
void setup() 
{
  size(500, 500); 
  smooth();
  noStroke();
 
  // populating the array
  // with new Ball objects
 for (int i = 0; i < NUM_BALLS; i++) 
  {
    myBalls[i] = new Ball(); 
 
    //assigning random positions  
    myBalls[i].posX = random(0, width);
    myBalls[i].posY = random(0, height);
    myBalls[i].speed = 2;
  } 
}
 
void draw() 
{ 
  background( 0, 0, 0);
 
  //call the method update every time I draw
  for (int i = 0; i < NUM_BALLS; i++) 
  {
    myBalls[i].update();
  }
 
 
}
 
 
class Ball
{
  //properties
  int myDiameter = 10;
  float posX = 250;
  float posY = 250;
  color col = #00FFFF;
  float speed = 0;
 
  //methods
  void update()
  {
  fill(col);
  ellipse(posX, posY, myDiameter, myDiameter);
  posX = posX + speed;
 
  //bouncing back and forth
  if(posX > width)
    {
    //it's better to instantly update the ball position to the max
    posX = width;
    speed *= -1;
    }
 
  if(posX < 0)
    {
    //it's better to instantly update the ball position to the max
    posX = 0;
    speed *= -1;
    }
  }
 
}
Posted in

Post a comment