I am working on this JavaScript code to get a ball to bounce off the walls and pause & resume on click. I have gotten the ball to pause and would like feedback on potential options to enable resume using a boolean. setTimer() & stopTimer() are predefined functions.
var ball;
var dx = 4;
var dy = 4;
var isPaused = false;
/* This program has a ball bounce around the screen. */
function start(){
ball = new Circle(20);
ball.setPosition(100, 100);
add(ball);
setTimer(draw, 20);
if(isPaused == true){
mouseClickMethod(resume);
}
}
function draw(){
checkWalls();
ball.move(dx, dy);
mouseClickMethod(pause);
}
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;
}
}
function pause(e){
var isPaused = true;
if(isPaused == true){
stopTimer(draw);
}
}
function resume(e){
var isPaused = false;
if(isPaused == false){
setTimer(draw, 20);
}
}