xxxxxxxxxx
// THEN 1203, 1205, 1208
// Object-Oriented Programming (OOP):
// programming using objects that are pieces of computer code and contain both functions and data
// objects in JavaScript are accessed through CLASSES
// classes have methods which are functions, properties which are variables, and instantiation
// instantiation: a constructor function which runs when you make one
var brickk = []; // array to hold all the bricks
function setup() {
createCanvas(windowWidth, windowHeight);
background(100);
for(let i = 0; i < 200; i++)
{
brickk[i] = new BRICK(random(width), random(height), 80, 20);
}
}
function draw() {
for(let i = 0;i < brickk.length;i++)
{
brickk[i].draw();
}
}
// a class definition:
class BRICK {
constructor(x=width/2,y=height/2,w=10,h=10) // run "new BRICK" each time and create a brick with the following functions
{
// set some properties: // "this" is for things inside the class
this.x = x;
this.y = y;
this.w = w;
this.h = h;
this.r = random(128,255);
this.g = random(128,255);
this.b = random(128,255);
}
// add some methods:
draw()
{
fill(this.r,this.g,this.b);
rect(this.x,this.y,this.w,this.h);
}
}