My if statement is not working, like last time but this time the line won't x = x - 2;
in my if statement (I didn't do the else if
because I thought it's the problem). Link: https://www.khanacademy.org/computer-programming/probability/5609184564559872. Code:
var x = 78;
var y = 18;
// Just in case I test
var saveX = 78;
var saveY = 18;
draw = function() {
background(150, 255, 250);
strokeWeight(1);
stroke(0, 0, 0);
rect(78, 20, 254, 25);
strokeWeight(6);
stroke(255, 0, 0);
line(x, y, x, y + 29);
if (x > 332) {
x = x - 2;
}
if (x < 78) {
x = x + 2;
}
x = x + 2;
};
x = x+2
makes the value is not changing at allThis is what you want for a ping-pong effect, key is the direction variable.
var x = 78;
var y = 18;
// Just in case I test
var saveX = 78;
var saveY = 18;
var direction = 1;
draw = function() {
background(150, 255, 250);
strokeWeight(1);
stroke(0, 0, 0);
rect(78, 20, 254, 25);
strokeWeight(6);
stroke(255, 0, 0);
line(x, y, x, y + 29);
if (x > 332) {
direction = -1;
}
if (x < 78) {
direction = 1;
}
x = x + direction * 2;
};