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

[AS3]as3的碰撞测试(以像素精度测试)代码示例

时间:2012-09-20 17:24CuPlayer
[AS3]as3的碰撞测试(以像素精度测试)代码示例,as3碰撞,as3碰撞检测

 

  1. //cuplayer像素级碰撞检测  
  2. package   
  3. {  
  4. import flash.display.BitmapData;  
  5. import flash.display.BlendMode;  
  6. import flash.display.DisplayObject;  
  7. import flash.display.Sprite;  
  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. * @param target1 DisplayObject  
  18. * @param target2 DisplayObject  
  19. * @param accurracy Number 检测的精度 [0 , 1] 0 <= n <= 1  
  20. * @return Boolean   
  21. */  
  22. public static function complexHitTestObject ( target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1 ):Boolean  
  23. {  
  24. accurracy > 1 ? accurracy = 1 : 0;  
  25. return accurracy <= 0?false:complexIntersectionRectangle(target1,target2,accurracy).width != 0;  
  26. }  
  27.    
  28. /**  
  29. * 极酷播放器提示:获取矩形边框重叠区域  
  30. * @param target1 DisplayObject  
  31. * @param target2 DisplayObject  
  32. * @return Rectangle  
  33. */  
  34. public static function intersectionRectangle ( target1:DisplayObject, target2:DisplayObject ):Rectangle  
  35. {  
  36. // If either of the items don't have a reference to stage, then they are not in a display list  
  37. // or if a simple hitTestObject is false, they cannot be intersecting.  
  38. if ( !target1.root || !target2.root || !target1.hitTestObject( target2 ) )  
  39. {  
  40. return new Rectangle ;  
  41. }  
  42.    
  43. // Get the bounds of each DisplayObject.  
  44. var bounds1:Rectangle = target1.getBounds( target1.root );  
  45. var bounds2:Rectangle = target2.getBounds( target2.root );  
  46.    
  47. // Determine test area boundaries.  
  48. var intersection:Rectangle = new Rectangle();  
  49. intersection.x = Math.max( bounds1.x, bounds2.x );  
  50. intersection.y = Math.max( bounds1.y, bounds2.y );  
  51. intersection.width = Math.min( ( bounds1.x + bounds1.width ) - intersection.x, ( bounds2.x + bounds2.width ) - intersection.x );  
  52. intersection.height = Math.min( ( bounds1.y + bounds1.height ) - intersection.y, ( bounds2.y + bounds2.height ) - intersection.y );  
  53.    
  54. return intersection;  
  55. }  
  56.    
  57. /**  
  58. * 极酷播放器提示:获取像素重叠区域  
  59. * @param target1 DisplayObject  
  60. * @param target2 DisplayObject  
  61. * @param accurracy Number 精度  
  62. * @return Rectangle  
  63. */  
  64. public static function complexIntersectionRectangle ( target1:DisplayObject, target2:DisplayObject, accurracy:Number = 1 ):Rectangle  
  65. {  
  66. if ( accurracy <= 0 )  
  67. {  
  68. throw new Error("ArgumentError: Error #5001: Invalid value for accurracy",5001);  
  69. }  
  70.    
  71. // If a simple hitTestObject is false, they cannot be intersecting.  
  72. if ( !target1.hitTestObject( target2 ) )  
  73. {  
  74. return new Rectangle ;  
  75. }  
  76.    
  77. var hitRectangle:Rectangle = intersectionRectangle( target1, target2 );  
  78. // If their boundaries are no interesecting, they cannot be intersecting.  
  79. if ( hitRectangle.width * accurracy <1 || hitRectangle.height * accurracy <1 )  
  80. {  
  81. return new Rectangle ;  
  82. }  
  83.    
  84. var bitmapData:BitmapData = new BitmapData( hitRectangle.width * accurracy, hitRectangle.height * accurracy, false, 0x000000 );  
  85.    
  86. // Draw the first target.  
  87. bitmapData.draw ( target1, HitTest.getDrawMatrix( target1, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) );  
  88. // Overlay the second target.  
  89. bitmapData.draw ( target2, HitTest.getDrawMatrix( target2, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, 255, 255, 255 ), BlendMode.DIFFERENCE );  
  90.    
  91. // Find the intersection.  
  92. var intersection:Rectangle = bitmapData.getColorBoundsRect( 0xFFFFFFFF,0xFF00FFFF );  
  93.    
  94. bitmapData.dispose ();  
  95.    
  96. // Alter width and positions to compensate for accurracy  
  97. if ( accurracy != 1 )  
  98. {  
  99. intersection.x /= accurracy;  
  100. intersection.y /= accurracy;  
  101. intersection.width /= accurracy;  
  102. intersection.height /= accurracy;  
  103. }  
  104. intersection.x += hitRectangle.x;  
  105. intersection.y += hitRectangle.y;  
  106.    
  107. return intersection;  
  108. }  
  109.    
  110. /**  
  111. * 极酷播放器提示:获取MC的矩阵  
  112. * @param target DisplayObject  
  113. * @param hitRectangle Rectangle  
  114. * @param accurracy Number  
  115. * @return Matrix  
  116. */  
  117. protected static function getDrawMatrix ( target:DisplayObject, hitRectangle:Rectangle, accurracy:Number ):Matrix  
  118. {  
  119. var localToGlobal:Point;  
  120. var matrix:Matrix;  
  121. var rootConcatenatedMatrix:Matrix = target.root.transform.concatenatedMatrix;  
  122.    
  123. localToGlobal = target.localToGlobal( new Point( ) );  
  124. matrix = target.transform.concatenatedMatrix;  
  125. matrix.tx = localToGlobal.x - hitRectangle.x;  
  126. matrix.ty = localToGlobal.y - hitRectangle.y;  
  127.    
  128. matrixmatrix.a = matrix.a / rootConcatenatedMatrix.a;  
  129. matrixmatrix.d = matrix.d / rootConcatenatedMatrix.d;  
  130. if ( accurracy != 1 )  
  131. {  
  132. matrix.scale ( accurracy, accurracy );  
  133. }  
  134. return matrix;  
  135. }  
  136. }  

 

热门文章推荐

请稍候...

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

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