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


Hitting the Blocks

The blocks across the screen are controlled programmatically. Each block is an instance of the same block. A color effect is applied to each to change the color of the blocks. In addition, the following script is applied to each block:

onClipEvent (load) {
   startX = this._x

This script is used to tell the block to hide if it is crossed by the ball movie clip. The only difference between each block is the instance name; each block has a different name that starts with the word Tile and is followed by a number between 1 and 35.

At the top of the script, you tell the game how many blocks are on the stage:

TileNum = 36;

You then tell Flash how to calculate that the object just hit is a block:

function maketargetList (numOfTiles) {
   for (i=0; i<numOfTiles; i++) {
     TargetList[i] = String("tile"+(i+1));

The final step is to add the script that will do something when a block when it is hit:

   //check if ball hits a tile
   var theTileNum = TargetList.length;
   for (i=0; i<theTileNum; i++) {
     var thisTile = TargetList[i];
     if (this.MovieClip.hitTest(_root[thisTile])) {
   Total += (10 * CurrentBallNum);
   _root[thisTile]._x += -1000;
        TargetList.splice(i, 1);
   if (TargetList.length == 0) {

Here the script verifies that a target is hit and then increases the games score by 10 points after the collision. The complete game can be seen in Figure 3.

Figure 3Figure 3 The final game.

BreakOut demonstrates collision with a boundary and with a static object.

6. Colliding Onwards | Next Section Previous Section