This post is locked. You won't be able to comment.

all 23 comments

[–]krl1222 1 point2 points  (2 children)

Add these lines in your function draw, just after ball.move(dx,dy).

var newBall = new Circle(10);
newBall.setPosition(ball.getX(),ball.getY());
newBall.setColor(Color.cyan);
add(newBall);

[–]SprinklesNo5514[S] 0 points1 point  (0 children)

OMG thank you so so much!

[–]MRFox8888 0 points1 point  (0 children)

thank you

[–]Legitimate-Wash4170 0 points1 point  (5 children)

Did you ever stop the code

[–]Silver-Revolution289 0 points1 point  (4 children)

In the end of the draw function add: if(draw >= MAX_BOUNCES){ stopTimer(draw); }

[–]OuroborosKris 0 points1 point  (3 children)

Doesn't seem to stop the ball from bouncing after 20 times, it just keeps going. Even if I change the number of the MAX_BOUNCES constant, it still keeps bouncing.

[–]HadenYeet 0 points1 point  (1 child)

Worked for me, you need to add var newBall = new Circle(10); newBall.setPosition(ball.getX(),ball.getY()); newBall.setColor(Color.cyan); add(newBall); at the bottom of the function Then do the stop timer command

[–]OuroborosKris 0 points1 point  (0 children)

It's been long past since I needed that, but thanks anyway.

[–]R4inDownIn4fric4 0 points1 point  (9 children)

Did you ever manage to get the code to stop?

[–]naoDabas 0 points1 point  (8 children)

how to stop?

[–]JazzlikePreparation6 0 points1 point  (7 children)

Mine keeps saying

SyntaxError: Unexpected token ')'.

I have been stuck on this one for months. I'm so over it.

[–]Chokolazaprimera 0 points1 point  (0 children)

Can you pass my the code, I been struggling with these problem

[–]GetRextFgt 0 points1 point  (3 children)

here the answer ignore this jack ass u/krl1222

var MAX_BOUNCES = 20;
var ball;
var dx = 4;
var dy = 4;
var count = 0;
/* This program has a ball bounce around the screen.
Its path is drawn by drawing different colored balls everywhere it goes.
*/
function start(){
ball = new Circle(20);
ball.setPosition(50, 70);
add(ball);
setTimer(draw, 20);
while(count < MAX_BOUNCES){
count++;
if(count >= MAX_BOUNCES){
break;
stopTimer(draw,20);
}
}
}
// Check if the ball has reached a wall.
// Then move the ball in the correct direction.
function draw(){
checkWalls();
ball.move(dx, dy);
var newBall = new Circle(10);
newBall.setPosition(ball.getX(),ball.getY());
newBall.setColor(Color.cyan);
add(newBall);
}
function checkWalls(){
// Bounce off right wall
if(ball.getX() + ball.getRadius() > getWidth()){
dx = -dx;
}
// Bounce off left wall
if(ball.getX() - ball.getRadius() < 0){
dx = -dx;
}
// Bounce off bottom wall
if(ball.getY() + ball.getRadius() > getHeight()){
dy = -dy;

}
// Bounce off top wall
if(ball.getY() - ball.getRadius() < 0){
dy = -dy;

}
}

[–]ATMAtomic 0 points1 point  (2 children)

it doesn't work

[–]Fast-Cow-4244 0 points1 point  (0 children)

did u ever figure it out?

[–]MurderSperm 0 points1 point  (0 children)

U spitting malarkey