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

Home > Articles > Web Design & Development > Adobe Flash

Collision Detection with Flash MX

  • Print
  • + Share This
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

Action games and arcade games all require that you have a character that, at some point, has to hit or collide with another object on the stage. This is referred to as collision detection: the ability for Flash to know when two or more objects hit each other. Flash has several tools that allow you to control object collision. In this article you will learn the following:

  • What collision is and how to use it

  • How to build game pieces to collide with each other

  • How to add effects to objects that collide with each other

  • How to use the hitTest method

  • How to define physical boundaries to a game

How to develop complex worlds in which game sprites can collide with many objects

Collision detection is all about the detection of objects and boundaries on the stage. In this article, you learn about collision detection used on my favorite game: BreakOut.

Almost always the first element you need to be able to define when scripting a game is the physical area of the game. This allows you, the programmer, to define where play can take place. For a marble game, this prevents marbles from dropping off the edge of a board; in a role-playing game, you define the area of the campaign; and with an arcade game, you know where the asteroids are going to come from. To more clearly define border collision and objects in relation to a border, we will be using the classic arcade game BreakOut.

BreakOut is a 1976 classic that first appeared on Atari and then was cloned a thousand times over. It remains one of the true paradigm games and makes for the perfect example of boundary collision detection. The basic premise of the game is to bat a ball around a screen and hit blocks. Each block you hit is knocked out, giving you points. When all of the blocks are gone, you proceed to the next level. In our game, we'll assume that the player has three lives—when all of the lives are gone, the game ends.

Area Detection: Where Can You Play?

The first step is to define where the ball will be bouncing. Our BreakOut game uses a movie size of 400px x 500px. This defines the size of the movie, not the area of the game. A ball can appear to bounce off the edge of the screen only if we tell the ball that there is an edge. This has to be defined programmatically with ActionScript.

The first scripts that will be added will be in Frame 1 of the movie. To add them, double-click on Frame 1 to open the Actions palette. Next, switch over to Expert mode by using the drop-down menu in the upper-right corner.

Now let's start out by creating a variable called GamePlayArea, with the values calculated in an array. It sounds complicated, but here's what it looks like:

GamePlayArea = [0, 0, 400, 500];

Essentially, you're telling Flash that the game covers an area 400px x 500px from the top-left corner of the screen. As you move through the ActionScript, you can reference the variable named GamePlayArea if you need to know the coordinates of the area of the game.

  • + Share This
  • 🔖 Save To Your Account