16.2k views
2 votes
We extend our draw function to include:

1 var ball;
2 var moveCounter = 0;
3 function draw(){
4
5 (2, 2);
6 moveCounter++;
7
8 if(moveCounter == 25){
9 stopTimer(draw);
10 }
11 }
12
13 function start(){
14 ball = new Circle(40);
15 add(ball);
14 setTimer(draw, 20);
15 }
How many times will the ball be moved before the animation stops?

User Tyler Lee
by
7.2k points

1 Answer

3 votes

Final answer:

The ball will be moved 25 times before the animation stops, as the draw() function increments the moveCounter variable with each call and stops the timer once it reaches 25.

Step-by-step explanation:

The question pertains to a code snippet that is part of a programming challenge involving the movement of a ball object on a screen. The movement is controlled by a function called draw(), which is set to be called repeatedly by setTimer(draw, 20). The variable moveCounter is incremented every time the draw() function is called. The animation stops when the moveCounter reaches 25, which is the condition set in the if statement. Since the timer is set to call the draw() function every 20 milliseconds, and the draw() function is called one last time to reach the count of 25 before calling stopTimer(draw), the ball will be moved 25 times before the animation stops.

User Estevan
by
7.6k points