Archive

Posts Tagged ‘gskinner’

AS 3 Hit Detection

September 13th, 2009 admin No comments

Get Adobe Flash player

Get the source example here.

What’s hit detection?

Hit detection is the ability to determine whether or not two DisplayObjects are colliding.

How’s it work?

It can be achieved any number of ways. Generally speaking, there are two ways of accomplishing this.

The first is the most basic, and limited, and it is to check whether or not two objects are colliding by comparing the coordinates of their bounding boxes, and determining if they overlap. The reason this is limiting is, the default bounding box of a MovieClip is a square, so if you have a non rectangular-shaped movie clip, this type of hit detection may detect a “hit” even if the clip isn’t visually colliding with the compared clip.

Bounding Box Example

Bounding Box

The second, is to use GCollision, a static ActionScript 3.0 Utility class written by Grant Skinner. The above example swf is created using the GCollision hit detection class. What’s nice about this class, is it using true, pixel to pixel, hit detection. In a nutshell, the way it works, is the two clips are converted to bitmaps with opposing colors, a ColorTransform is applied to the bitmap, resulting in a bitmap that shows one of the clips entirely red, the other entirely green, and colliding pixels (if there are any) in yellow. If there is even a pixel of yellow in the resulting image, then collision has occurred.

So lets all say thanks GSkinner!

Get the source example here.

Categories: Tutorial Tags: , , ,