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.
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 |
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 |
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
Returns the number of display objects in the collision list, including the target display object.
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.
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):
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
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.
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:
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.
ParametersRemoves 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
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.
Replaces the target display object with the display object specified.
ParametersAll 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:
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:
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:
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; } }