Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Design > Adobe Creative Suite

  • Print
  • + Share This
Like this article? We recommend

Controlling the Bouncing Ball

With the gameplay area now defined, you need to add the ball that will bounce around the stage. It is important that the ball is referenced correctly; otherwise, the code won't work.

The first thing to drag onto the stage is an instance of the movie clip ball. Name the instance of the ball as ball, as shown in Figure 1.

Figure 1Figure 1 Here you can see the movie clip named ball. This clip will interact with the screen by bouncing off walls you will define.


With the ball instance named, you can now begin adding the ActionScript that controls the behavior of the ball. All of the script that controls the ball is placed in the first frame of the movie, along with the GamePlayArea declaration. The first part of the script controls how the ball moves:

newBall.prototype.moveBall = function () { 
   this.MovieClip._x += this.deltaX;
   this.MovieClip._y += this.deltaY;
   //check if ball hits top of screen
   if (this.MovieClip._y<0) {
     this.flipY();
   }
   if (this.MovieClip._x<GamePlayArea[0] || this.MovieClip._
x>GamePlayArea[2]) {
     this.flipX();
   }
}
   if (this.MovieClip._x<GamePlayArea[0] || this.MovieClip._
x>GamePlayArea[2]) {
     this.flipX();
   }

Here the movie instance ball knows where the area of the game is by referencing the variable GamePlayArea. Earlier, you told Flash that the game area is only 400 x 500. The ball will now bounce off an imaginary wall that is 400 x 500.

  • + Share This
  • 🔖 Save To Your Account