Home / Articles / Adobe Flash / Collision Detection with Flash MX

Collision Detection with Flash MX

Article Description

Collision detection is all about detecting objects and boundaries on the stage. Matthew David illustrates how to use collision detection in an arcade-style game.

Like this article? We recommend

Macromedia Flash MX Game Design Demystified

Macromedia Flash MX Game Design Demystified

$49.99

Get Ready to be Paddled

To test this out, add a paddle to the game, as shown in Figure 2. You'll be able to control the movement of the paddle with your arrow keys. The paddle will hit the ball and bounce it off the walls. You will need to name the paddle movie clip thePaddle (see Figure 2).

Figure 2Figure 2 The paddle enables you to hit the ball as it falls down. This uses the hitTest method.


The following script adds the paddle movement:

   for(thisPad=0;thisPad<2;thisPad++){
     var thePaddle = ThePlayer[thisPad];
     if (this.MovieClip.hitTest(thePaddle) && this.deltaY > 0) {
        this.paddleHit(thePaddle);
     }
   }

Here, the movie clip method hitTest is used to allow an object to physically collide with another. In this case, the ball can collide with the paddle. After a collision has occurred, an event can be triggered. In this instance, the value to the variable deltaY is triggered, causing the ball to be sent back up the screen.

4. Life Is Short | Next Section Previous Section