// Declare and construct two objects (h1 and h2) of the class HLine
var h1 = new HLine(20, 1.0);
var h2 = new HLine(50, 5.0);
function setup() {
createCanvas(100, 100);
//background(200);
}
function draw() {
if (h2.speed > 1.0) { // Dot syntax can be used to get a value
h2.speed -= 0.01; // or set a value.
}
h1.update(); // Calls the h1 object's update() function
h2.update(); // Calls the h2 object's update() function
// Not part of the original code. I added this here to
// show how to expose a variable, that otherwise would be
// private to the object.
// Look within the constructor for the ypos() function
console.log(h1.ypos())
}
function HLine(y, s) { // Class definition and Constructor
var ypos = y;
var speed = s;
this.update = function() { // Update method
ypos += speed;
if (ypos > width) {
ypos = 0;
}
line(0, ypos, width, ypos);
}
// Not part of the original code. I added this here to
// show how to expose a variable, that otherwise would be
// private to the object.
// Look within the draw() function where I console.log the result of the function
this.ypos = function(){
return ypos;
}
}