Created
October 3, 2015 17:01
-
-
Save claytical/28b8c611e92d1b9ec9e2 to your computer and use it in GitHub Desktop.
Ball Object Example with Methods
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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