...
Classes are one of the most important programming constructs in many programming languages. They are the abstract model or blueprint for an object. 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 functions methods, but otherwise, they work pretty much the same as the variables and functions we already know. In order 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.
...
The constructor is the initialisation function of a class. This function is called when an 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.
...
Methods are the functions of a class.
Child classes - Keyword 'extends'
A class can be derived from another class, keeping all the original's qualities and allowing us to make new ones. This new class inherits the methods and attributes of 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 call the constructor of a base class (in the case of 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
...
Example BallClass
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.
Code Block |
---|
var ball1; var ball2; var p1; var p2; var drag = false; function setup() { createCanvas(800,600); p1= createVector(); p2= createVector(); ball1 = new BouncingBall(100,100, 30); ball2 = new BouncingBall(50,50); imageMode(CENTER); smooth(, 10); } function draw() { background(255); fill(255,255,255,60); rect(0,0,width,height); if(drag) { // draw the direction of shoot p1.set(ball1._pos.x,ball1._pos.y,0); line(p1.x,p1.y,p2.x,p2.y); } // draw the balls ball1.draw(); ball2.draw(); } function mousePressedmouseClicked() { drag = true; p1.set(ball1._pos.x,ball1._pos.y,0); p2.set ball1.setDirection(mouseX, mouseY,0); } function mouseDragged() { p2.setball2.setDirection(mouseX,mouseY,0); } function mouseReleased() { drag = false; // shooting direction calculation. var dir = p5.Vector.sub(p2,p1); // shorten the length dir.mult(0.09); //the ball is given a new direction ball1.set(p1,dir,0.993); ball2.set(p2,dir,0.95 mouseY); } |
Example BouncingBall
Code Block |
---|
class BouncingBall { // constructor constructor(shapeWidth, shapeHeightx,y,radius) { this._posx = createVector(width/2, height/2)x; this._diry = createVector(0,0)y; this.velocityX = 1; this._dampVvelocityY = 1; this._wdampV = shapeWidth0.99; this._hgravity = shapeHeight1.9; this._shaper = loadImage("ball.png")radius; } // set the new position + direction + dampening setsetDirection(postargetX,dir,dampVtargetY) { let newVelocityX = targetX-this._pos = pos; x let newVelocityY = targetY-this.y this._dir.add(dir)velocityX = newVelocityX *0.10; this._dampVvelocityY = dampV newVelocityY*0.10; } // update the current position calcPos() { // add gravity this.velocityY += this.gravity; // curentapply positionfriction shiftedto velocity this._pos.add(velocityX = this.velocityX * this._dir)dampV; this.velocityY = this.velocityY * this._dampV; // movementadd velocity vectorto modifiedposition this._dir.mult(x += this._dampV)velocityX; this.y += this.velocityY; // test horisontal if(this._pos.x + this._w/2r > width) { this._dir.xvelocityX *= -1; this._pos.x = width - this._w/2r; } else if(this._pos.x - this._w/2r < 0) { this._dir.xvelocityX *= -1; this._pos.x = this._w/2r; } // test vertical if(this._pos.y + this._h/2r > height) { this._dir.yvelocityY *= -1; this._pos.y = height - this._w/2r; } else if(this._pos.y - this._h/2r < 0) { this._dir.yvelocityY *= -1; this._pos.y = this._h/2r; } } // draw the ball draw() { this.calcPos(); imagecircle(this._shape,this._pos.x,this._pos.y,this._w,this._hr); } } |
Exercise A
Modify the program , so that several many new balls can be placed independently of each other.
Exercise B
Extend the class again to make a third kind of ball. Modify its attributes and methods to create new kinds of behavior.
are placed on the screen. Using an array and a for loop, it’s possible to quickly add hundreds balls!
Child classes - Keyword 'extends'
A class can be derived from another class, keeping all the originald qualities and allowing us to make new ones. 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 call the constructor of a base class from 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
Code Block | ||
---|---|---|
| ||
class BallEx extends BouncingBall { constructor(size) { super(size,size); } function draw() { super.draw(); line(0, 0 , this._pos.x,this._pos.y); } } |