检测碰撞实例
一个基于as3的碰撞检测类:
三个参数(第一个对象:DisplayObject、
第二个对象:DisplayObject、精度:Number),
返回一个布尔值
package { import flash.display.BitmapData; import flash.display.BlendMode; import flash.display.DisplayObject; import flash.display.Sprite; import flash.geom.ColorTransform; import flash.geom.Matrix; import flash.geom.Point; import flash.geom.Rectangle; public class HitTest { public static function complexHitTestObject( target1:DisplayObject, target2:DisplayObject, accuracy:Number = 1 ):Boolean { return complexIntersectionRectangle( target1, target2, accuracy ).width != 0; } public static function intersectionRectangle( target1:DisplayObject, target2:DisplayObject ):Rectangle { // If either of the items don't have a reference to stage, then they are not in a display list // or if a simple hitTestObject is false, they cannot be intersecting. if (! target1.root || ! target2.root || ! target1.hitTestObject(target2)) { return new Rectangle(); } // Get the bounds of each DisplayObject. var bounds1:Rectangle = target1.getBounds(target1.root); var bounds2:Rectangle = target2.getBounds(target2.root); // Determine test area boundaries. var intersection:Rectangle = new Rectangle(); intersection.x = Math.max(bounds1.x,bounds2.x); intersection.width = Math.min( ( bounds1.x + bounds1.width ) - intersection.x, ( bounds2.x + bounds2.width ) - intersection.x ); intersection.height = Math.min( ( bounds1.y + bounds1.height ) - intersection.y, ( bounds2.y + bounds2.height ) - intersection.y ); return intersection; } public static function complexIntersectionRectangle( target1:DisplayObject, target2:DisplayObject, accuracy:Number = 1 ):Rectangle { if (accuracy <= 0) { throw new Error("ArgumentError: Error #5001: Invalid value for accuracy",5001); } // If a simple hitTestObject is false, they cannot be intersecting. if (! target1.hitTestObject(target2)) { return new Rectangle(); } var hitRectangle:Rectangle = intersectionRectangle(target1,target2); // If their boundaries are no interesecting, they cannot be intersecting. if (hitRectangle.width * accuracy < 1 || hitRectangle.height * accuracy < 1) { return new Rectangle(); } var bitmapData:BitmapData = new BitmapData(hitRectangle.width * accuracy,hitRectangle.height * accuracy,false,0x000000); // Draw the first target. bitmapData.draw( target1, HitTest.getDrawMatrix( target1, hitRectangle, accuracy ), new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) ); // Overlay the second target. bitmapData.draw( target2, HitTest.getDrawMatrix( target2, hitRectangle, accuracy ), new ColorTransform( 1, 1, 1, 1, 255, 255, 255, 255 ), BlendMode.DIFFERENCE ); // Find the intersection. var intersection:Rectangle = bitmapData.getColorBoundsRect(0xFFFFFFFF,0xFF00FFFF); bitmapData.dispose(); // Alter width and positions to compensate for accuracy if (accuracy != 1) { intersection.x /= accuracy; intersection.y /= accuracy; intersection.width /= accuracy; intersection.height /= accuracy; } intersection.x += hitRectangle.x; intersection.y += hitRectangle.y; return intersection; } protected static function getDrawMatrix( target:DisplayObject, hitRectangle:Rectangle, accuracy:Number ):Matrix { var localToGlobal:Point;; var matrix:Matrix; var rootConcatenatedMatrix:Matrix = target.root.transform.concatenatedMatrix; localToGlobal = target.localToGlobal( new Point( ) ); matrix = target.transform.concatenatedMatrix; matrix.tx = localToGlobal.x - hitRectangle.x; matrix.ty = localToGlobal.y - hitRectangle.y; matrix.a = matrix.a / rootConcatenatedMatrix.a; matrix.d = matrix.d / rootConcatenatedMatrix.d; if (accuracy != 1) { matrix.scale( accuracy, accuracy ); } return matrix; } } }
package { import flash.display.Sprite; import flash.events.MouseEvent; import flash.geom.Rectangle; public class ComplexIntersectionRectangle extends Sprite { private var _circle0:Sprite; private var _circle1:Sprite; private var _hit:Boolean; private var _intersection:Sprite; public function ComplexIntersectionRectangle() { super(); initialize(); } private function initialize():void { _circle0 = new Sprite(); _circle0.x = 50; _circle0.y = 100; _circle0.addEventListener( MouseEvent.MOUSE_DOWN, onCircleMouseDown ); draw( _circle0, 0xFFFFFF ); addChild( _circle0 ); _circle1 = new Sprite(); _circle1.x = 250; _circle1.y = 100; _circle1.addEventListener( MouseEvent.MOUSE_DOWN, onCircleMouseDown ); draw( _circle1, 0xFFFFFF ); addChild( _circle1 ); _intersection = new Sprite(); addChild( _intersection ); _hit = false; stage.addEventListener( MouseEvent.MOUSE_MOVE, onStageMouseMove ); stage.addEventListener( MouseEvent.MOUSE_UP, onStageMouseUp ); } private function onCircleMouseDown( event:MouseEvent ):void { var circle:Sprite = Sprite(event.target); circle.startDrag( false ); } private function onStageMouseMove( event:MouseEvent ):void { var intersection:Rectangle = HitTest.complexIntersectionRectangle(_circle0,_circle1,5); var hit:Boolean = ( intersection.width > 0 && intersection.height > 0 ); _intersection.graphics.clear(); if (hit) { _intersection.graphics.beginFill( 0xFF0000, 0.5 ); _intersection.graphics.drawRect( intersection.x, intersection.y, intersection.width, intersection.height ); _intersection.graphics.endFill(); } if (hit != _hit) { _hit = hit; var color:Number = ( _hit ) ? 0x00FF00 : 0xFFFFFF; draw( _circle0, color ); draw( _circle1, color ); } } private function onStageMouseUp( event:MouseEvent ):void { _circle0.stopDrag(); _circle1.stopDrag(); } private function draw( circle:Sprite, color:Number ):void { circle.graphics.clear(); circle.graphics.beginFill( color, 1 ); circle.graphics.drawCircle( 0, 0, 25 ); circle.graphics.endFill(); } } }