Properties  Methods  Examples  Index
Package com.coreyoneil.collision
Class public class CollisionList
Inheritance CollisionList —> CDK —> Object

Language Version: Actionscript 3.0
Player Version: Flash Player 9

The CollisionList class lets you perform pixel-precise (shape-based) collision detections for display objects in a one-to-many relationship. All display objects added to a collision list will check for collisions against a single target display object that you specify. CollisionList works with all display objects, including MovieClips, Sprites, Bitmaps, TextFields, and FLVs.

BitmapData is generated for display objects inside a collision list instance. This data is then used to find any overlapping objects on the stage. This is performed using logic extended from the CDK class.

CollisionList accounts for transformations (scale, rotation, color transforms, etc.) made to instances of objects. It can approximate the angle of collision between two objects based on their shapes where the collision occurred, and tell you how much overlap there is between the two objects.

CollisionList is unique from its sister class CollisionGroup in that it only checks for collisions against a single display object. For example, if you had four display objects in a collision list - let's call them A, B, C, and D - and the target you want to check for collisions against, there would be four collision checks made:

At least one display object must be added to a collision list (minus the target) before you may check for collisions. Display objects that you no longer want to check against for collisions can be removed from a collision list using the removeItem() method. The target can be changed using the swapTarget() method.


Public Properties

Property Defined By
alphaThreshold : Number
A floating point value from 0 to 1 that determines the minimum alpha value to check for collisions.
CDK
numChildren : uint
[read-only] Returns the number of display objects in the collision list.
CDK
returnAngle : Boolean
If set to true, CollisionList will return an approximated angle of each collision that's detected.
CDK
returnAngleType : String
A value that determines if the angle returned from a collision is in degrees or radians.
CDK


Public Methods

Method Defined By
CollisionList(target:DisplayObject, ... objs)
Creates a CollisionList object with optional display object parameters.
CollisionList
addItem(obj:DisplayObject)
Adds a display object to the collision list's list of objects to check for collisions.
CDK
checkCollisions():Array
Takes every possible pairing of display objects in the collision list and checks for collisions. If a pair of objects have collided, they are recorded and returned by checkCollisions() as an array.
CollisionList
excludeColor(theColor:uint, alphaRange:uint = 255, redRange:uint = 20, greenRange:uint = 20, blueRange:uint = 20)
Defines a color or color range to exclude from collision detections. The excludeColor() method expects a 32 bit color value. Optional ranges based on this color can be specified for all channels using the additional parameters.
CDK
removeExcludeColor(theColor:uint)
Removes the color specified from the list of colors to exclude from collision detections. Expects a 32 bit color value.
CDK
removeItem(obj:DisplayObject)
Removes the specified display object from the collision list.
CollisionList
swapTarget(target:DisplayObject)
Replaces the target display object with the display object specified.
CollisionList



Property Detail


alphaThreshold  property
alphaThreshold:Number [read-write]

A floating point value from 0 to 1 that determines the minimum alpha value to check for collisions. The default setting is 0.

If alphaThreshold were set to .5, then CollisionList would ignore any collisions that occurred where either of the display objects' overlapping pixels had an alpha equal to or below .5

View an Example

Implementation
     public function get alphaThreshold():Number
     public function set alphaThreshold(theAlpha:Number)

numChildren  property
numChildren:uint [read-only]

Returns the number of display objects in the collision list, including the target display object.

Implementation
     public function get numChildren():uint

returnAngle  property
returnAngle:Boolean [read-write]

If set to true, CollisionList will return an approximated angle of each collision that's detected. The default setting is false.

The angle of collision for each collision is returned by the checkCollisions() method.

Implementation
     public function get returnAngle():Boolean
     public function set returnAngle(option:Boolean)

See Also
     returnAngleType
     checkCollisions()

returnAngleType  property
returnAngleType:String [read-write]

A value that determines if the angle returned from a collision is in degrees or radians. The following values are accepted by returnAngleType (not case-sensitive):

The default setting is RADIANS.



Implementation
     public function get returnAngle():Boolean
     public function set returnAngle(option:Boolean)

See Also
     returnAngle


Method Detail


CollisionList()  Constructor
public function CollisionList(target:DisplayObject, ... objs)

Creates a CollisionList object, defines the list's target display object, and allows for optional display object parameters.

If you want to add display objects to the collision list during instantiation, you can pass them into the constructor. For example, if you had a target display object named "target", and you had three display objects called A, B, and C that you wanted to add for collision detection against target, you could create the list like this:

var collisions:CollisionList = new CollisionList(target, A, B, C);

Parameters
     target : DisplayObject — The display object all other display objects in the collision list will check against for collisions.

     ... objs — Optional list of display objects to add to the collision list.

Throws
     Error — Object passed isn't a valid display object


addItem()  method
public function addItem(obj:DisplayObject):void

Adds a display object to the collision list's list of objects to check for collisions. To add a new target display object, use the swapTarget() method.

Parameters
     obj : DisplayObject — The display object to add to the collision list.

Throws
     Error — Object added isn't a valid display object

See Also
     removeItem()

checkCollisions()  method
public function checkCollisions():Array

Takes every display object in the collision list and checks for collisions against the target display object. If a pair of objects have collided, they are recorded and returned by checkCollisions() as an array.

The array returned by checkCollisions() is an array of objects representing each collision, with each object containing the following properties:

Returns
    Array — An array containing information for each collision that was recorded. Each element in the array is an object containing the two display objects
                      that collided, their angle of collision, and the overlapping points between the two display objects.


See Also
     returnAngle

excludeColor()  method
public function excludeColor(theColor:uint, alphaRange:uint = 255, redRange:uint = 20, greenRange:uint = 20, blueRange:uint = 20):void

Defines a color or color range to exclude from collision detections. The excludeColor() method expects a 32 bit color value. Optional ranges based on this color can be specified for all channels using the additional parameters. Each range accepts integral values from 0 to 255, accounting for all values for each color channel. The ranges move in both directions at the amount specified for each channel. For example, if you had a collision list named "collisions" and called excludeColor() like so:

collisions.excludeColor(0xFFCCAADD, 0, 0, 20, 0);

... then all pixels with colors between 0xFFCC95DD and 0xFFCCBDDD would be excluded from collisions. Pixels are only excluded from collisions if all four channels (ARGB) are within their respective ranges. You may exclude as many colors/ranges as you want by making multiples calls to excludeColor().

Because the Collision Detection Kit performs its detections based on pixel values, it's not uncommon for pixels on the edge of a display object to have alpha values lower than the main color of its shape. This is caused by anti-aliasing performed on the display object. Because of this, the excludeColor() method defaults its ranges so that it accounts for all alpha values, and provides a small range of 20 for red, green, and blue. This accounts for most discrepancies, allowing you to simply pass the color value you wish to exclude without needing to adjust ranges.

excludeColor() is CPU-intensive. It's suggested that you only exclude one or two colors when working with larger display objects or a large number of display objects in your collision list. Whenever possible, use alphaThreshold instead.

View an Example

Parameters
     theColor : uint — The color to exclude. Must be a 32 bit value (Example: 0xFF663322)

     alphaRange : uint (default = 255) — The integral range of alpha values to exclude, based on the color's alpha value. Valid values are 0 - 255

     redRange : uint (default = 20) — The integral range of red values to exclude, based on the color's red value. Valid values are 0 - 255

     greenRange : uint (default = 20) — The integral range of green values to exclude, based on the color's green value. Valid values are 0 - 255

     blueRange : uint (default = 20) — The integral range of blue values to exclude, based on the color's blue value. Valid values are 0 - 255

See Also
     removeExcludeColor()

removeExcludeColor()  method
public function removeExcludeColor(theColor:uint):void

Removes the color specified from the list of colors to exclude from collision detections. Must be a color previously added using the excludeColor() method. Expects a 32 bit color value.

Parameters
     theColor : uint — The color to remove from exclusions. Must be a 32 bit value (Example: 0xFF663322)

Throws
     Error — Color specified isn't in the exclusion list.

See Also
     excludeColor()

removeItem()  method
public override function removeItem(obj:DisplayObject):void

Removes the specified display object from the collision list. This cannot be used to remove the target display object. To change the target display object, use the swapTarget() method.

Parameters
     obj : DisplayObject — The display object to remove from the collision list.

Throws
     Error — Display object isn't in the collision list
     Error — Cannot remove target display object from the collision list

See Also
     addItem()
     swapTarget()

swapTarget()  method
public function swapTarget(target:DisplayObject):void

Replaces the target display object with the display object specified.

Parameters
     target : DisplayObject — The display object to set as the new target display object.

Throws
     Error — Object isn't a valid display object


Examples


All of the following examples make use of a simple setup to show how to make use of CollisionList. To run them, you will have to have downloaded the Collision Detection Kit and included it in your list of classpaths.

In this first example, CollisionList is used to detect collisions between a target display object and four additional display objects. The target in this example is a Sprite made of two overlapping ellipses. The four display objects that will check for collisions against this Sprite are four Sprites containing circles. The user can drag the circles around the stage, and when any of them collide with the ellipses, a text box displays which circle made the collision. This task is accomplished using the following steps:

Copy and paste the code into the timeline of a new FLA and compile to see it in action.

import com.coreyoneil.collision.CollisionList;

var messageBox:TextField = new TextField();
addChild(messageBox);

var target:Sprite = new Sprite();
addChild(target);

target.graphics.beginFill(0xFF0000);
target.graphics.drawEllipse(0, 0, 120, 80);
target.graphics.drawEllipse(20, -20, 80, 120);
target.graphics.endFill();

target.x = stage.stageWidth / 1.5;
target.y = stage.stageHeight / 2 - 40;

var collisionList:CollisionList = new CollisionList(target);

for(var i:uint = 0; i < 4; i++)
{
	var circle:Sprite = new Sprite();
	circle.name = "circle " + i;
	addChild(circle);

	circle.graphics.beginFill(0x0000FF);
	circle.graphics.drawCircle(0, 0, 20);
	circle.graphics.endFill();
	
	circle.x = 40;
	circle.y = stage.stageHeight / 4 * i + 40;
	
	circle.buttonMode = true;
	
	circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown_Handler);
	
	collisionList.addItem(circle);
}

function mouseDown_Handler(e:MouseEvent):void
{
	var circle:Sprite = e.currentTarget as Sprite;
	stage.addChild(circle);
	e.currentTarget.startDrag();
	
	e.currentTarget.addEventListener(MouseEvent.MOUSE_MOVE, checkForCollision);
	e.currentTarget.addEventListener(MouseEvent.MOUSE_UP, mouseUp_Handler);
}

function mouseUp_Handler(e:MouseEvent):void
{
	e.currentTarget.stopDrag();
	
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_MOVE, checkForCollision);
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, mouseUp_Handler);
}

function checkForCollision(e:MouseEvent):void
{
	var collisions:Array = collisionList.checkCollisions();
	
	messageBox.text = "";

	for(var i:uint = 0; i < collisions.length; i++)
	{
		var circle:Sprite = collisions[i].object1;
		messageBox.appendText("Collision detected on " + circle.name + "\n");
		messageBox.autoSize = "center";
		messageBox.x = stage.stageWidth / 2 - messageBox.width / 2;
	}
}


This example shows the basic usage of the alphaThreshold property. Sprites containing circles will be set up and placed in a CollisionList instance so that they can be dragged over a target display object with two alpha values. The Sprites will ignore the lower alpha value of the target display object, and only detect collisions with the more opaque value. All collisions are reported via a text field. This is accomplished through the following steps:

Copy and paste the code into the timeline of a new FLA and compile to see it in action.

import com.coreyoneil.collision.CollisionList;

var messageBox:TextField = new TextField();
addChild(messageBox);

var target:Sprite = new Sprite();
addChild(target);

target.graphics.beginFill(0xFF0000, .25);
target.graphics.drawCircle(0, 0, 80);
target.graphics.endFill();

target.graphics.beginFill(0xFF0000, 1);
target.graphics.drawCircle(0, 0, 40);
target.graphics.endFill();

target.x = stage.stageWidth / 1.5;
target.y = stage.stageHeight / 2;

var collisionList:CollisionList = new CollisionList(target);

collisionList.alphaThreshold = .25;

for(var i:uint = 0; i < 4; i++)
{
	var circle:Sprite = new Sprite();
	circle.name = "circle " + i;
	addChild(circle);

	circle.graphics.beginFill(0x0000FF);
	circle.graphics.drawCircle(0, 0, 20);
	circle.graphics.endFill();
	
	circle.x = 40;
	circle.y = stage.stageHeight / 4 * i + 40;
	
	circle.buttonMode = true;
	
	circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown_Handler);
	
	collisionList.addItem(circle);
}

function mouseDown_Handler(e:MouseEvent):void
{
	var circle:Sprite = e.currentTarget as Sprite;
	stage.addChild(circle);
	e.currentTarget.startDrag();
	
	e.currentTarget.addEventListener(MouseEvent.MOUSE_MOVE, checkForCollision);
	e.currentTarget.addEventListener(MouseEvent.MOUSE_UP, mouseUp_Handler);
}

function mouseUp_Handler(e:MouseEvent):void
{
	e.currentTarget.stopDrag();
	
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_MOVE, checkForCollision);
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, mouseUp_Handler);
}

function checkForCollision(e:MouseEvent):void
{
	var collisions:Array = collisionList.checkCollisions();
	
	messageBox.text = "";

	for(var i:uint = 0; i < collisions.length; i++)
	{
		var circle:Sprite = collisions[i].object1;
		messageBox.appendText("Collision detected on " + circle.name + "\n");
		messageBox.autoSize = "center";
		messageBox.x = stage.stageWidth / 2 - messageBox.width / 2;
	}
}


This example shows the basic usage of the excludeColor() method. Sprites containing circles will be set up and placed in a CollisionList instance so that they can be dragged over a target display object with two color values. The Sprites will ignore one of the target display object's colors, and only detect collisions with the second color. All collisions are reported via a text field. This is accomplished through the following steps:

Copy and paste the code into the timeline of a new FLA and compile to see it in action.

import com.coreyoneil.collision.CollisionList;

var messageBox:TextField = new TextField();
addChild(messageBox);

var target:Sprite = new Sprite();
addChild(target);

target.graphics.beginFill(0xFF0000);
target.graphics.drawCircle(0, 0, 80);
target.graphics.endFill();

target.graphics.beginFill(0x00AA00);
target.graphics.drawCircle(0, 0, 40);
target.graphics.endFill();

target.x = stage.stageWidth / 1.5;
target.y = stage.stageHeight / 2;

var collisionList:CollisionList = new CollisionList(target);

collisionList.excludeColor(0xFFFF0000);

for(var i:uint = 0; i < 4; i++)
{
	var circle:Sprite = new Sprite();
	circle.name = "circle " + i;
	addChild(circle);

	circle.graphics.beginFill(0x0000FF);
	circle.graphics.drawCircle(0, 0, 20);
	circle.graphics.endFill();
	
	circle.x = 40;
	circle.y = stage.stageHeight / 4 * i + 40;
	
	circle.buttonMode = true;
	
	circle.addEventListener(MouseEvent.MOUSE_DOWN, mouseDown_Handler);
	
	collisionList.addItem(circle);
}

function mouseDown_Handler(e:MouseEvent):void
{
	var circle:Sprite = e.currentTarget as Sprite;
	stage.addChild(circle);
	e.currentTarget.startDrag();
	
	e.currentTarget.addEventListener(MouseEvent.MOUSE_MOVE, checkForCollision);
	e.currentTarget.addEventListener(MouseEvent.MOUSE_UP, mouseUp_Handler);
}

function mouseUp_Handler(e:MouseEvent):void
{
	e.currentTarget.stopDrag();
	
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_MOVE, checkForCollision);
	e.currentTarget.removeEventListener(MouseEvent.MOUSE_UP, mouseUp_Handler);
}

function checkForCollision(e:MouseEvent):void
{
	var collisions:Array = collisionList.checkCollisions();
	
	messageBox.text = "";

	for(var i:uint = 0; i < collisions.length; i++)
	{
		var circle:Sprite = collisions[i].object1;
		messageBox.appendText("Collision detected on " + circle.name + "\n");
		messageBox.autoSize = "center";
		messageBox.x = stage.stageWidth / 2 - messageBox.width / 2;
	}
}