xxxxxxxxxx
65
// from lesson by coding train
// you seen that you need a relational operator with some sport of
//boolean expression because something like mouseX>100 evaluates
//to true or false. A variable with type boolean, you dont need to
//check anything about it. it itself has the value true or false
// if(kyo){
// kyo =false ;
//} else{
// kyo= true;
// }
// what this code syas is on is true, on should be false , otherweise
//on should be true
// if its on, turn it off, otherwise it must have been off so turn if
//on
// so now you have a switch that can change the state of the program
// this can be condensed into one line of code using the
// not operator: !
//if(!on) {do something} . so i have a variable on = false
//does this code get executed?
// not false is true so !on evaluates to true
// on = !on . what does that mean? i want to set the value of this
// variable to not itself, so its value is false, not false is true
//if its value is true, not true is false
// this expressoin reverses a boolean expression, makes true false
//false true
var kyo = false;
function setup() {
createCanvas(600, 400);
}
function draw() {
if(kyo){
background(0,255,0);
} else{
background(0);
}
stroke(255);
strokeWeight(4);
noFill();
if(mouseX>250 && mouseX< 350 && mouseY>150 && mouseY<250){
fill(255,0,200);
}
rectMode(CENTER);
rect(300,200,100,100);
}
function mousePressed(){
if(mouseX>250 && mouseX< 350 && mouseY>150 && mouseY<250){
kyo=!kyo;
}
}
// when you see lines in your code taht are pretty much identical,
// you should ask yourself, is there a better way to do this?
// if(mouseIsPressed){
// background(0,255,0);
// }
//