读书人

as3.0中的碰撞重叠有关问题

发布时间: 2012-03-01 10:25:46 作者: rapoo

as3.0中的碰撞重叠问题
如果有两个不规则的图形经过鼠标拖拽,触发了hittestobject,重叠的面积以矩形的形状颜色加深,代码该如何写呀??
以下是碰撞检验,没有重叠面积加深的。

tuxing.addEventListener(MouseEvent.MOUSE_DOWN, tuo);

stage.addEventListener(MouseEvent.MOUSE_UP, ting);

function tuo(event:MouseEvent):void {
stage.addEventListener(MouseEvent.MOUSE_MOVE , dong);

}
function dong(event:MouseEvent):void {
tuxing.x=mouseX;
tuxing.y=mouseY;
if (tuxing.hitTestObject(tuxing2)) {
trace("碰到");
} else {
trace("没碰到");
}
}

function ting(event:MouseEvent):void {
stage.removeEventListener(MouseEvent.MOUSE_MOVE , dong);
}

[解决办法]
这个有点意思,AS中似乎没有计算出碰撞区域的直接方法,碰撞之后,通过hitTestPoint方法循环检测出最左点,最上点,最右点和最下点?似乎效率不怎么样,但是我想到的最简单的方法。
[解决办法]
得2点即可确定矩形
代码如下:

JScript code
          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.y    = Math.max( bounds1.y, bounds2.y );                        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;                }
[解决办法]
JScript code
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 {          //·µ»Ø2¸ö?¼þÏ?Ö?þ         public static function complexHitTestObject( target1:DisplayObject, target2:DisplayObject,  accurracy:Number = 1 ):Boolean {             return complexIntersectionRectangle(target1,target2,accurracy).width != 0;         }         //·µ»Ø2¸ö?¼þÒ?ØÐÎÏ?»ãµÄÇøÓò,         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.y    = Math.max( bounds1.y, bounds2.y );             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, accurracy:Number = 1 ):Rectangle {             if ( accurracy <= 0 ) {                 throw new Error("ArgumentError: Error #5001: Invalid value for accurracy",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 * accurracy <1 || hitRectangle.height * accurracy <1 ) {                 return new Rectangle  ;             }              var bitmapData:BitmapData = new BitmapData( hitRectangle.width * accurracy, hitRectangle.height * accurracy, false, 0x000000 );              // Draw the first target.             bitmapData.draw( target1, HitTest.getDrawMatrix( target1, hitRectangle, accurracy ), new ColorTransform( 1, 1, 1, 1, 255, -255, -255, 255 ) );             // Overlay the second target.             bitmapData.draw( target2, HitTest.getDrawMatrix( target2, hitRectangle, accurracy ), 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 accurracy             if ( accurracy != 1 ) {                 intersection.x /= accurracy;                 intersection.y /= accurracy;                 intersection.width /= accurracy;                 intersection.height /= accurracy;             }             intersection.x += hitRectangle.x;             intersection.y += hitRectangle.y;              return intersection;         }         protected static function getDrawMatrix( target:DisplayObject, hitRectangle:Rectangle, accurracy: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 ( accurracy != 1 ) {                 matrix.scale( accurracy, accurracy );             }              return matrix;         }     } } 

读书人网 >Flash

热点推荐