·您当前的位置:首页 > 技术教程 > AS2与AS3技术 >

[AS3]as3中HitTest碰撞检测源代码实例

时间:2014-06-27 09:40酷播
[AS3]as3中HitTest碰撞检测源代码实例

[AS3]as3中HitTest碰撞检测源代码实例

  1. package  
  2.          
  3.         import flash.display.BitmapData; 
  4.         import flash.display.BlendMode; 
  5.         import flash.display.DisplayObject; 
  6.         import flash.display.Sprite; 
  7.          
  8.         import flash.geom.ColorTransform; 
  9.         import flash.geom.Matrix; 
  10.         import flash.geom.Point; 
  11.         import flash.geom.Rectangle; 
  12.          
  13.         public class HitTest 
  14.         { 
  15.  
  16.                 /* 
  17.                     该方法返回 碰撞 true/false 
  18.                 */  
  19.                 public static function complexHitTestObject( target1:DisplayObject, target2:DisplayObject,  accurracy:Number = 1 ):Boolean 
  20.                 { 
  21.                         return complexIntersectionRectangle( target1, target2, accurracy ).width != 0; 
  22.                 } 
  23.                  
  24.                 public static function intersectionRectangle( target1:DisplayObject, target2:DisplayObject ):Rectangle 
  25.                 { 
  26.                         // If either of the items don't have a reference to stage, then they are not in a display list 
  27.                         // or if a simple hitTestObject is false, they cannot be intersecting. 
  28.                         if( !target1.root || !target2.root || !target1.hitTestObject( target2 ) ) return new Rectangle(); 
  29.                          
  30.                         // Get the bounds of each DisplayObject. 
  31.                         var bounds1:Rectangle = target1.getBounds( target1.root ); 
  32.                         var bounds2:Rectangle = target2.getBounds( target2.root ); 
  33.                          
  34.                         // Determine test area boundaries. 
  35.                         var intersection:Rectangle = new Rectangle(); 
  36.                         intersection.x   = Math.max( bounds1.x, bounds2.x ); 
  37.                         intersection.y    = Math.max( bounds1.y, bounds2.y ); 
  38.                         intersection.width      = Math.min( ( bounds1.x + bounds1.width ) - intersection.x, ( bounds2.x + bounds2.width ) - intersection.x ); 
  39.                         intersection.height = Math.min( ( bounds1.y + bounds1.height ) - intersection.y, ( bounds2.y + bounds2.height ) - intersection.y ); 
  40.                  
  41.                         return intersection; 
  42.                 } 
  43.                  
  44.                 public static function complexIntersectionRectangle( target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1 ):Rectangle 
  45.                 {                      
  46.                         if( accurracy <= 0 ) throw new Error( "ArgumentError: Error #5001: Invalid value for accurracy", 5001 ); 
  47.                          
  48.                         // If a simple hitTestObject is false, they cannot be intersecting. 
  49.                         if( !target1.hitTestObject( target2 ) ) return new Rectangle(); 
  50.                          
  51.                         var hitRectangle:Rectangle = intersectionRectangle( target1, target2 ); 
  52.                         // If their boundaries are no interesecting, they cannot be intersecting. 
  53.                         if( hitRectangle.width * accurracy <1 || hitRectangle.height * accurracy <1 ) return new Rectangle(); 
  54.                          
  55.                         var bitmapData:BitmapData = new BitmapData( hitRectangle.width * accurracy, hitRectangle.height * accurracy, false, 0x000000 );  
  56.  
  57.                         // Draw the first target. 
  58.                         bitmapData.draw( target1, HitTest.getDrawMatrix( target1, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) ); 
  59.                         // Overlay the second target. 
  60.                         bitmapData.draw( target2, HitTest.getDrawMatrix( target2, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, 255, 255, 255 ), BlendMode.DIFFERENCE ); 
  61.                          
  62.                         // Find the intersection. 
  63.                         var intersection:Rectangle = bitmapData.getColorBoundsRect( 0xFFFFFFFF,0xFF00FFFF ); 
  64.                          
  65.                         bitmapData.dispose(); 
  66.                          
  67.                         // Alter width and positions to compensate for accurracy 
  68.                         if( accurracy != 1 ) 
  69.                         { 
  70.                                 intersection.x /= accurracy; 
  71.                                 intersection.y /= accurracy; 
  72.                                 intersection.width /= accurracy; 
  73.                                 intersection.height /= accurracy; 
  74.                         } 
  75.                          
  76.                         intersection.x += hitRectangle.x; 
  77.                         intersection.y += hitRectangle.y; 
  78.                          
  79.                         return intersection; 
  80.                 } 
  81.                  
  82.                  
  83.                 protected static function getDrawMatrix( target:DisplayObject, hitRectangle:Rectangle, accurracy:Number ):Matrix 
  84.                 { 
  85.                         var localToGlobal:Point;; 
  86.                         var matrix:Matrix; 
  87.                          
  88.                         var rootConcatenatedMatrix:Matrix = target.root.transform.concatenatedMatrix; 
  89.                          
  90.                         localToGlobal = target.localToGlobal( new Point( ) ); 
  91.                         matrix = target.transform.concatenatedMatrix; 
  92.                         matrix.tx = localToGlobal.x - hitRectangle.x; 
  93.                         matrix.ty = localToGlobal.y - hitRectangle.y; 
  94.                          
  95.                         matrixmatrix.a = matrix.a / rootConcatenatedMatrix.a; 
  96.                         matrixmatrix.d = matrix.d / rootConcatenatedMatrix.d; 
  97.                         if( accurracy != 1 ) matrix.scale( accurracy, accurracy ); 
  98.  
  99.                         return matrix; 
  100.                 } 
  101.  
  102.         } 
  103.  

 

热门文章推荐

请稍候...

保利威视云平台-轻松实现点播直播视频应用

酷播云数据统计分析跨平台播放器