xxxxxxxxxx
39
// Declare the variable bob
let bob;
function setup() {
createCanvas(400, 400);
background(42);
// Add Properties (x, y and r)
// Add Methods (show and move)
bob = {
x: random(width),
y: random(height),
r: 10,
show: function () {
fill(255, 255, 0);
// We use the keyword 'this' to represent the object itself
// In this case the keyword 'this' refers to bob
circle(this.x, this.y, this.r);
},
move: function () {
this.x += random(-5, 5);
this.y += random(-5, 5);
},
};
}
function draw() {
// Call the methods show and move on the object bob
bob.show();
bob.move();
}
/* Challenges
- Add more objects.
- Add more properties to the objects you created
- Add one more method that checks if the bob is within the canvas and returns true or false. Use that method to only move bob if it is within the canvas.
- Add a new method of your liking
*/