1

嘿,我有碰撞检测问题我使用了网站http://www.freeactionscript.com/2009/05/pixel-perfect-collision-detection/上的这个例子,但它只在你的对象的注册点在上面时才有效左角 如果注册点在中心,我应该在代码中进行哪些更改?

这是代码:

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

public class CollisionTest 
{
// vars
private var _returnValue:Boolean;

private var _onePoint:Point;
private var _twoPoint:Point;

private var _oneRectangle:Rectangle;
private var _twoRectangle:Rectangle;

private var _oneClipBmpData:BitmapData;
private var _twoClipBmpData:BitmapData;

private var _oneOffset:Matrix;
private var _twoOffset:Matrix;

/**
 * Simple collision test. Use this for objects that are not rotated.
 * @param   clip1   Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
 * @param   clip2   Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
 * @return  Collision True/False
 */
public function simple(clip1:DisplayObjectContainer, clip2:DisplayObjectContainer):Boolean
{
    _returnValue = false;

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

    _twoRectangle = clip2.getBounds(clip2);
    _twoClipBmpData = new BitmapData(_twoRectangle.width, _twoRectangle.height, true, 0);
    _twoClipBmpData.draw(clip2);

    _onePoint = new Point(clip1.x, clip1.y)
    _twoPoint =  new Point(clip2.x, clip2.y)

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

    return _returnValue;
}

/**
 * Complex collision test. Use this for objects that are rotated, scaled, skewed, etc
 * @param   clip1   Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
 * @param   clip2   Takes DisplayObjectContainer as argument. Can be a Sprite, MovieClip, etc.
 * @return  Collision True/False
 */
public 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;
}

}
}
4

1 回答 1

2

好吧,让我们这样说:左上角离中心点有多远?

x += width  * .5;
y += height * .5;

正确的?

因此,如果您在上面发布的代码只用左上角处理它(无论出于何种原因,我不知道为什么)为什么不强制它用中心点来处理它呢?

也许是这样的?

_onePoint = new Point(clip1.x + _clip.width * .5, clip1.y + _clip.height * .5);
_twoPoint =  new Point(clip2.x + _clip2.width * .5, clip2.y + _clip2.height * .5);
于 2013-01-20T12:56:43.450 回答