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

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

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

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

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

 

热门文章推荐

请稍候...

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

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