xxxxxxxxxx
31
var rectX = 0;
var directionRight = true;
function setup() {
createCanvas(400, 400);
}
function draw() {
background(0);
fill(200, 0, 100);
rect(rectX, 200, 50, 50);
// Move the rectangle
if (directionRight){
rectX += 2;
}
else{
rectX -= 2;
}
// When the rectangle reaches the edge of the canvas,
// flip the direction boolean
if (rectX > width-50 || rectX < 0 ){
directionRight = !directionRight;
}
// For debugging
//console.log(rectX);
}