2

我正在开发一款游戏,其中在更改旋转和大小后形状必须发生碰撞。只要 image1 没有使用它的 .pivotX 和 .pivotY 旋转,hitTest 就可以工作,但是一旦我围绕枢轴旋转,碰撞就是错误的。我尝试使用矩阵但没有好的结果。任何人都可以帮助我吗?这里的代码:

package
{
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.geom.Matrix;
    import flash.geom.Point;
    import flash.geom.Rectangle;

    import starling.core.Starling;
    import starling.display.Image;
    import starling.display.Sprite;
    import starling.events.Event;
    import starling.textures.Texture;
    import starling.textures.TextureAtlas;

    public class HitTest extends Sprite
    {
        [Embed(source="src/image.png")]
        public static const ImagePng:Class;

        public function HitTest()
        {
            addEventListener(starling.events.Event.ADDED_TO_STAGE, init);
        }

        private function init():void{
            //First Image
            var bitmap1:Bitmap = new ImagePng();
            var texture1:Texture = Texture.fromBitmap(bitmap1);
            var image1:Image = new Image(texture1);
            addChild(image1);
            /*
            Here's the problem:
            If you don't use .pivotX and .pivotY image1 doesn't rotate around its center but hits image2
            If you use .pivotX and .pivotY image1 rotates around its center but the hittest doesn't work.
            */
        //  image1.pivotX = image1.width/2;
        //  image1.pivotY = image1.height/2;
            image1.x=215;
            image1.y=50;
            image1.rotation=1;

            var rect1:Rectangle = image1.getBounds(this);
            var offset1:Matrix = new Matrix;

            offset1.rotate(1);

            offset1.tx = image1.x - rect1.x;
            offset1.ty = image1.y - rect1.y;

            var bitmapData1:BitmapData = new BitmapData(rect1.width, rect1.height, true, 0);
            bitmapData1.draw(bitmap1, offset1); 

            //Second Image
            var bitmap2:Bitmap = new ImagePng();
            var texture2:Texture = Texture.fromBitmap(bitmap2);
            var image2:Image = new Image(texture2);
            addChild(image2);
            image2.x=270;
            image2.y=-10;

            var rect2:Rectangle = image2.getBounds(this);

            var offset2:Matrix = new Matrix;
            offset2.tx = image2.x - rect2.x;
            offset2.ty = image2.y - rect2.y;    

            var bitmapData2:BitmapData = new BitmapData(rect2.width, rect2.height, true, 0);
            bitmapData2.draw(bitmap2, offset2); 

            var point1:Point = new Point(rect1.x, rect1.y);
            var point2:Point = new Point(rect2.x, rect2.y);

            //Hit Test
            if(bitmapData1.hitTest(point1,255,bitmapData2,point2,255))
            {
                image2.color=0x00ff00;
            }
        }
    }
}
4

1 回答 1

0

This is again with matrix, but I think it works pretty well (it's somehow old, but the idea remains the same): http://www.mikechambers.com/blog/2009/06/24/using-bitmapdata-hittest-for-collision-detection/

于 2014-04-21T12:19:32.650 回答