I'm completely new to JavaScript and p5.js. I know this is not the most efficient code for bouncing ball, but I tried to write my own bouncing ball mechanic as self-learning.
function draw() {
background (col); // background
col = map(cir.x, 0, 600, 0, 255) // map function
fill (350, 200, 200);
ellipse (cir.x, cir.y, cir.diameter, cir.diameter);
// Bounding ball mechanic
var speed = 3;
var hitRightWall = false;
var hitLeftWall = false;
if (cir.x >= 300) {
hitRightWall == true;
hitLeftWall == false;
}
else if (cir.x <= -50) {
hitLeftWall == true;
hitRightWall == false;
}
if (hitRightWall==true) {
speed == -3;
}
else {
speed == 3;
}
cir.x = cir.x + speed;
}
For some reason, the if (hitRightWall==True)
condition is never activated even though if (cir.x >= 300)
was activated. The ball keeps going to the right and off the screen.