xxxxxxxxxx
48
var harry; // creating variables to house our object
var sally;
function setup() {
createCanvas(windowWidth, windowHeight);
background(232, 250, 210);
noStroke();
harry = new Face(); // here we are instantiating the Face object (note that we aren't using a createFish() type of method and the use of the keyword new.
sally = new Face();
}
function draw() {
background(232, 250, 210);
harry.display(); // using dot notation as usual!
sally.display();
if (mouseIsPressed) {
harry.diameter *= 1.05;
sally.xPos += 10;
}
}
function Face() { // a constructor object
// these lines set up the properties, note the difference between the way properties are declared in the fish object
this.xPos = random(width);
this.yPos = random(height);
this.diameter = random(40, 80);
this.b = random(0,255);
this.r = random(0,255);
// these lines set up the methods
this.display = function() {
// face
fill(this.r, 150, this.b);
ellipse(this.xPos, this.yPos, this.diameter, this.diameter);
// eyes
fill(0);
ellipse(this.xPos + 10, this.yPos, 5,8);
ellipse(this.xPos - 10, this.yPos, 5,8);
ellipse(this.xPos, this.yPos+10, 8, 5);
}
}