In this tutorial we are going to implement a simple PacMan-like game engine using the last movie clip and basically the method hitTest(). The user can move the object with the arrows of the keyboard and our PacMan eventually 'eats' movie clips. We added two movie clips: one for the pipes or walls and another for the pills. Please remember this is a game engine, and for this reason it still can be improved and extended.
Movie clips
As we said, we added two movie clips. One is for the walls --the limits of the game (Figure 1). You can make it as complex as you like. The second one is the pill that our PacMan can eat (Figure 2). As you can see, the creation of both movie clips is trivial.

Figure 1. You can make this movie as complex as you desire.

Figure 2. This is the pill that our character can 'eat'.
ActionScript
Please drag the movie clips from the Library window to Scene 1. You need to instance the movie clips ball and pipes as pacman and pipes. You do not need to instance the movie clip pill.
Please right-click on a instance of the movie clip pill and write:
|
onClipEvent(enterFrame) { if (_root.pacman.hitTest(this)) {
_visible=false; this.removeMovieClip(); } } |
It is simple to see: using the method hitTest(), we will check for collision detection. We can use this function in two ways: with or without bounding boxes. As we mentioned in previous tutorials, a bounding box surrounds the object and represents it in a simpler way, as a rectangle. In this case we use it with bounding boxes. Then, we check if the two bounding boxes -one for the pacman and the other for the pill- are overlapped. The keyword this represents in this case to the pill. If there is collision, we hide and destroy the current pill. To add pills to the stage, you can copy and paste the instance you dragged. This will automatically create new instances with the same actions. Place them between the pipes.
Now let us edit the actions of the instance pacman:
onClipEvent (load)
{
EDEV_SPEED=3;
EDEV_LEFT=0;
EDEV_RIGHT=180;
EDEV_UP=90;
EDEV_DOWN=270;
EDEV_direction=EDEV_LEFT;
}
onClipEvent(enterFrame)
{
if (Key.isDown(Key.LEFT))
EDEV_direction=EDEV_LEFT;
else if (Key.isDown(Key.RIGHT))
EDEV_direction=EDEV_RIGHT;
else if (Key.isDown(Key.UP))
EDEV_direction=EDEV_UP;
else if (Key.isDown(Key.DOWN))
EDEV_direction=EDEV_DOWN;
switch (EDEV_direction)
{
case EDEV_LEFT:
if (!_root.pipes.hitTest(_x-_width*0.5, _y, true))
_x-=EDEV_SPEED;
if (_rotation!=EDEV_LEFT)
_rotation=EDEV_LEFT;
break;
case EDEV_RIGHT:
if (!_root.pipes.hitTest(_x+_width*0.5, _y, true))
_x+=EDEV_SPEED;
if (_rotation!=EDEV_RIGHT)
_rotation=EDEV_RIGHT;
break;
case EDEV_UP:
if (!_root.pipes.hitTest(_x, _y-_height*0.5, true))
_y-=EDEV_SPEED;
if (_rotation!=EDEV_UP)
_rotation=EDEV_UP;
break;
case EDEV_DOWN:
if (!_root.pipes.hitTest(_x, _y+_height*0.5, true))
_y+=EDEV_SPEED;
if (_rotation!=EDEV_DOWN)
_rotation=EDEV_DOWN;
break;
}
} |
The first event handler has got to be familiar to you. It was not changed. About the second event handler, if you analyze the code, there is not difference with the previous code, except for:
_root.pipes.hitTest(parameter1, parameter2, parameter3);
You can see we are using an overloaded hitTest(). In this case it takes three parameters: two numbers and a Boolean. The concept is very simple: because the value of the flag is true, it checks if the point (parameter1, parameter2) of a certain object overlaps any point of the movie clip pipes. When the Boolean is true, it means that the method does not use bounding boxes, it uses the exact shape of the movie instead. You might note we add or subtract the width or height of the instance. This is because we use the extreme points. Let us give an example. If the object is moving up, it is reasonable that we check the upper limit of the object (_y-_height*0.5). The other cases are similar. But please note this simple case does not exactly fill all the cases. You can add your own conditionals to improve the game engine.
Conclusion
Using the method hitTest() with two versions, we could create a simple game engine. It can be improved and there are cases that need to be thought, but because this is a game engine, we did not develop them.
The source files for this tutorial may be downloaded here.