xxxxxxxxxx
48
//Global Variable//
let x = 20;
let msg = 'hello world';
let emptyMsg; // Just declared the variable, name is registered
//declare fishes
let fish1;
let fish2;
function setup() {
createCanvas(400, 400);
// print(x);
//let msg = 'I am awesome'; //Local Variable; overwrites global variable//
//print (msg) //in setup
fish1 = new fish()
print ('fish1.x -' + fish1.x)
fish1.name = 'nemo'
fish1.print ()
fish2 = new fish()
print ('fish2.x -' + fish2.x)
fish2.name = 'sushi'
fish2.print()
}
function draw() {
background(220);
print (msg);//global one because there's not a message declared here//
print(emptyMsg);
emptyMsg = 'now defined' // do not use let for changing variables mid function
print(emptyMsg)
noLoop();
}
//fish have a position
class fish{
constructor(){
print('constructing a fish');
this.x = random(0, width)
}
print(){
print (this.name + ' x - ' + this.x)
}
}