Bouncing Ball!


By combining if and else if statements, we created a way to bounce a ball on the stage with ActionScript. Even better, by converting the speed to a variable, we were able to use a classic trick to reverse the course of a moving object: multiple the variable speed by -1.

We also made it easier to customize speed by abstracting to a variable: It's right there at the top of our code! And in the future, that variable can change at runtime (which means the ball can increase or decrease speed as the project runs)

  1. Created a file called ball_bounce
  2. Reversed the direction the ball moves by adding a negative number
  3. Created an else if statement to reverse again
  4. Created a variable called speed
  5. Flipped the value of speed by multiplying -1

Important Actions

else if(ball.y<boundaryTop){

Self-Check for Understanding

In plain English, what do you think the following code does?

Check an explanation here