ActionScript 3的位图Hittest碰撞检查具有偏移的动画片段

时间:2017-07-12 22:15:50

标签: actionscript-3 bitmap collision-detection movieclip hittest

我无法让位图hittest方法在我的动画片段上正常工作。我尝试了几次使用来自不同来源的信息,但都没有效果。我遇到了一个预先制作的功能,可以解决所有最棘手的困惑,我尝试使用它。我首先尝试了一个新程序并发现为了使函数工作,动画片段必须位于左上角,但考虑到我必须在我的动画片段上使用特定的旋转点,以左上角为中心不是一个选择。我也尝试改变计算偏移的方式,看看我是否可以操纵每个偏移到左上角,但它似乎没有任何好处。

我知道这是一个非常具体的问题,但是我没有足够的关于位图测试的信息让我自己理解这一点,我认为这对于其他试图使用和理解精确碰撞的人来说也是有用的。

以下是预制功能的代码:

var _returnValue:Boolean;
var _onePoint:Point;
var _twoPoint:Point;
var _oneRectangle:Rectangle;
var _twoRectangle:Rectangle;
var _oneClipBmpData:BitmapData;
var _twoClipBmpData:BitmapData;
var _oneOffset:Matrix;
var _twoOffset:Matrix;
function complex(clip1:DisplayObjectContainer, clip2:DisplayObjectContainer):Boolean
{
            _returnValue = false;

            _twoRectangle = clip1.getBounds(clip1);
            _oneOffset = clip1.transform.matrix;
            _oneOffset.tx = clip1.x - clip2.x;
            _oneOffset.ty = clip1.y - clip2.y;

            _twoClipBmpData = new BitmapData(_twoRectangle.width, _twoRectangle.height, true, 0);
            _twoClipBmpData.draw(clip1, _oneOffset);        

            _oneRectangle = clip2.getBounds(clip2);
            _oneClipBmpData = new BitmapData(_oneRectangle.width, _oneRectangle.height, true, 0);

            _twoOffset = clip2.transform.matrix;
            _twoOffset.tx = clip2.x - clip2.x;
            _twoOffset.ty = clip2.y - clip2.y;

            _oneClipBmpData.draw(clip2, _twoOffset);

            _onePoint = new Point(_oneRectangle.x, _oneRectangle.y);
            _twoPoint = new Point(_twoRectangle.x, _twoRectangle.y);

            if(_oneClipBmpData.hitTest(_onePoint, 255, _twoClipBmpData, _twoPoint, 255))
            {
                _returnValue = true;
            }

            _twoClipBmpData.dispose();
            _oneClipBmpData.dispose();

            return _returnValue;
}

以下是调用预制函数的代码:

var inVision:Boolean = complex(visionVector[controlBirds], birdVector[checkLRD]);

以下是动画片段的截图:

Bird movieclip (The movieclip referenced by the birdVector)

Vision movieclip (The movieclip referenced by the visionVector)

非常感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

您分享的功能相当简单。它会从每个MovieClip和checks the alpha channel for overlapping pixels创建一个位图。如您所述,偏移是您的问题,您需要更改您采样的区域。

阅读BitmapData.draw()的API,您可以看到第二个参数接受矩阵。使用Matrix.translate(),我们可以将样本区域从默认0,0坐标偏移(尽管matrix.txmatrix.ty是等效属性。)

要注意的第二件事是传递给Point的{​​{1}}个对象定义了开始被测试的两个hitTest()对象重叠的位置(因为它们没有与舞台有任何关系,我们必须定义这些虚构物体的位置)。该位置必须是BitmapData

以下是重构函数...

[location of object] + [bounding offset]