Hi there!
Been using the java platform and recentley i’ve started to use the p5 platform. Really cool!
However, I have this function for a bouncing ball that works perfectly in Java but doesnt work on js and I cant find the error!
-a- posting code please use the </> Preformatted text button from the editor menu,
-b- but as you want use p5.js please link to your project ( or MVCS ) at https://editor.p5js.org/
so we see the whole thing and can play with it.
i noted you use x and this.x
what possibly is not same.
The problem’s that the bubbles should bounce once they get to the edge of the canvas but instead they keep mooving and dissapear.
The class is in another js file, here it is:
class Bubble {
constructor(x, y, r) {
this.x = x
this.y = y
this.r = r
}
display() {
stroke(127);
// fill(100,100,100);
ellipse(this.x, this.y, this.r*2 );
}
moveX() {
let xSpeed = 1;
this.x = this.x + xSpeed;
if (this.x > width - this.r*2 || this.x < this.r*2) {
xSpeed = xSpeed *-1
}
//console.log(xSpeed)
}
moveY() {
let ySpeed = random(2);
this.y = this.y - ySpeed;
if (this.y > height- this.r*2 || this.y < this.r*2) {
ySpeed = ySpeed *-1
}
}
overlaps(other) {
let d = dist(this.x, this.y, other.x, other.y);
if (d <= this.r + other.r) {
return true;
} else {
return false;
}
}
}
again, now we can work online
( no need to post code )
and regarding your question i make a copy ( file / duplicate ) of yours,
reduced it down to your class move question
( and cleaned up ) https://editor.p5js.org/kll/sketches/8c0ZTMlsZ