Skip to content

Instantly share code, notes, and snippets.

@claytical
Created October 3, 2015 17:01
Show Gist options
  • Save claytical/28b8c611e92d1b9ec9e2 to your computer and use it in GitHub Desktop.
Save claytical/28b8c611e92d1b9ec9e2 to your computer and use it in GitHub Desktop.
Ball Object Example with Methods
var Ball = function(x, y) {
this.x = x;
this.y = y;
this.diameter = 10;
}
Ball.prototype.show = function() {
ellipse(this.x, this.y, this.diameter, this.diameter);
}
Ball.prototype.fall = function() {
this.y = this.y + 1;
}
var balls = [];
function setup() {
createCanvas(windowWidth, windowHeight);
}
function draw() {
background(0);
for (var i = 0; i < balls.length; i++) {
balls[i].show();
balls[i].fall();
}
}
function mousePressed() {
balls.push( new Ball(mouseX, mouseY) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment