Collision detection

Collisions are an inevitable fact of life in an animated world. So far, the movies we created do not know how to detect when two objects have collided or what to do when they do. This section focuses on the first issue: collision detection. Responding properly to collisions will be treated in detail in Chapter 11.

In general, collision detection can be a tricky problem, but if the objects to be tested are rectangular in shape, there is a very simple built-in method: hitTestObject().

Using the hitTestObject() method

The hitTestObject() method determines whether two display objects (such as sprites or movie clips) are in collision:

object1.hitTestObject(object2);

The outcome is a Boolean: it returns true if they do and false ...

Get The Essential Guide to Physics for Flash Games, Animation, and Simulations now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.