Classes are one of the most important constructs in many programming languages, allowing us to use the object-oriented paradigm to organise code and make it more understandable. Classes are an abstract model or blueprint for an object. An object, like in the real world, can literally be anything, and all objects have properties and functions. And, like the real world, our code can consist almost entirely of interacting objects.
We can also understand classes as extended data types. Unlike the primitive data types we have already looked at, classes define both variables and functions. Programmers call class variables attributes and class function methods, but otherwise, they work pretty much the same as the variables and functions we already know. To get access to these attributes and methods, we need to use a dot syntax, i.e. "." must be placed after the object name, followed by the name of the attribute or the method. For example car.speed might store the speed attribute of an object and the car.drive() method might be a method for moving the car around the screen.
Constructor
The constructor is the initialisation function of a class. This function is called when a new instance (a new object) of a class is created.
An object is created with the command 'new' and the subsequent call to the constructor.
Attribute
Attributes are the variables of a class.
Methods
Methods are the functions of a class.
The basic principle of a class can be understood and used fairly quickly, and in some regards it Object Oriented Coding lends itself to a more Intuitive understanding of code. However, there is a lot of nuance and theory to writing a good class and it takes considerable practice.
Below the draw function, you will find the BouncingBall
class. It’s common practice to separate classes into their own file, to help keep code organised.
var ball1; var ball2; function setup() { createCanvas(800,600); ball1 = new BouncingBall(100,100, 30); ball2 = new BouncingBall(50,50, 10); } function draw() { background(255); fill(255,255,255,60); // draw the balls ball1.draw(); ball2.draw(); } function mouseClicked() { ball1.setDirection(mouseX, mouseY); ball2.setDirection(mouseX, mouseY); } class BouncingBall { // constructor constructor(x,y,radius) { this.x = x; this.y = y; this.velocityX = 1; this.velocityY = 1; this._dampV = 0.99; this.gravity = 1.9; this.r = radius; } // set the new position + direction + dampening setDirection(targetX,targetY) { let newVelocityX = targetX-this.x let newVelocityY = targetY-this.y this.velocityX = newVelocityX *0.10; this.velocityY = newVelocityY*0.10; } // update the current position calcPos() { // add gravity this.velocityY += this.gravity; // apply friction to velocity this.velocityX = this.velocityX * this._dampV; this.velocityY = this.velocityY * this._dampV; // add velocity to position this.x += this.velocityX; this.y += this.velocityY; // test horisontal if(this.x + this.r > width) { this.velocityX *= -1; this.x = width - this.r; } else if(this.x - this.r < 0) { this.velocityX *= -1; this.x = this.r; } // test vertical if(this.y + this.r > height) { this.velocityY *= -1; this.y = height - this.r; } else if(this.y - this.r < 0) { this.velocityY *= -1; this.y = this.r; } } // draw the ball draw(){ this.calcPos(); circle(this.x,this.y,this.r); } }
Modify the program so that many new balls are placed on the screen. Using an array and a for loop, it’s possible to quickly add hundreds balls!
Object-Oriented Coding (using classes to organise code) provides a number of powerful ways to use classes and objects. One more advanced feature is inheritance, where a class can be based on an existing class. This new class inherits the methods and attributes of the base class. However, if new methods or attributes in the child class have the same names as those in the base class, the base class properties are overwritten. To reference the constructor of a base class from a child class, the keyword 'super' is used.
The structure of the child class:
class className extends basicClass
{
[Attributes - Variables]
[Constructor – Initialising function]
[Methods- Functions]
}
Example BallClass Extended
class BallEx extends BouncingBall { constructor(size) { super(size,size); } function draw() { super.draw(); line(0, 0 , this.x,this.y); } }