对象之间的内部命中测试将检查对象的边界框,因此这对您不起作用。
如果你能以某种方式使用玩家代理作为一个点(最低点,比如他的脚中部或类似的东西)你可以使用spike.hitTestPoint(globalFootX, globalFootY, true);
如果这不起作用,您必须手动为项目创建 hittest-representation 并执行您自己的 hittest 逻辑。
另一种解决方案是将项目绘制到单独的精灵上,然后查看像素是否重叠。我知道我在一个旧的 AS2 项目中做到了这一点,其中不规则形状的 AI 机器人在一个不规则形状的世界中四处移动。
我将提供该代码以供参考,它可能可以转换为 AS3,或者至少您可以将其用作谷歌搜索的灵感,以便在 AS3 中找到该解决方案。
class messer_studios.utils.CollisionDetection {
static public function checkForCollision(p_clip1:MovieClip, p_clip2:MovieClip, p_alphaTolerance:Number):Rectangle {
// set up default params:
if (p_alphaTolerance == undefined) {
p_alphaTolerance = 255;
}
// get bounds:
var bounds1:Object = p_clip1.getBounds(_root);
var bounds2:Object = p_clip2.getBounds(_root);
// rule out anything that we know can't collide:
if (((bounds1.xMax < bounds2.xMin) || (bounds2.xMax < bounds1.xMin)) || ((bounds1.yMax < bounds2.yMin) || (bounds2.yMax < bounds1.yMin))) {
return null;
}
//Debug.log("might collide");
// determine test area boundaries:
var bounds:Object = {};
bounds.xMin = Math.max(bounds1.xMin, bounds2.xMin);
bounds.xMax = Math.min(bounds1.xMax, bounds2.xMax);
bounds.yMin = Math.max(bounds1.yMin, bounds2.yMin);
bounds.yMax = Math.min(bounds1.yMax, bounds2.yMax);
// set up the image to use:
var img:BitmapData = new BitmapData(bounds.xMax-bounds.xMin, bounds.yMax-bounds.yMin, false);
// draw in the first image:
var mat:Matrix = p_clip1.transform.concatenatedMatrix;
mat.tx -= bounds.xMin;
mat.ty -= bounds.yMin;
img.draw(p_clip1, mat, new ColorTransform(1, 1, 1, 1, 255, -255, -255, p_alphaTolerance));
// overlay the second image:
mat = p_clip2.transform.concatenatedMatrix;
mat.tx -= bounds.xMin;
mat.ty -= bounds.yMin;
img.draw(p_clip2, mat, new ColorTransform(1, 1, 1, 1, 255, 255, 255, p_alphaTolerance), "difference");
// find the intersection:
var intersection:Rectangle = img.getColorBoundsRect(0xFFFFFFFF, 0xFF00FFFF);
// if there is no intersection, return null:
if (intersection.width == 0) {
return null;
}
// adjust the intersection to account for the bounds:
intersection.x += bounds.xMin;
intersection.y += bounds.yMin;
return intersection;
};
public static function hitTestShape(mc1:MovieClip, mc2:MovieClip, alphaTolerence:Number):Boolean {
return checkForCollision(mc1, mc2, alphaTolerence) != null ? true : false;
}
}