Want to remember if something is on or off? Want to toggle (switch) between two states? Booleans have your back. Booleans are variables which hold one of two values: true or false.
Try this code:
Try clicking on the canvas. What happens? Why?
Want to remember if something is on or off? Want to toggle (switch) between two states? Booleans have your back. Booleans are variables which hold one of two values: true or false.
Try this code:
Try clicking on the canvas. What happens? Why?
let day = false; // day is a boolean, set to false
function setup() {
createCanvas(400, 400);
}
function draw() {
if(day == false)
{
background(255); // if day is true
}
else
{
background(40); // if day is false
}
}
function mousePressed() {
// ! means "not", and it gives the opposite of the value
// so !false is actually "not false"... which means true
day = !day;
print("day is " + day);
}