Actionscript 3 将电影剪辑大小调整为3

Actionscript 3 将电影剪辑大小调整为3,actionscript-3,resize,movie,clip,Actionscript 3,Resize,Movie,Clip,好的,我在flash中有一个游戏,一个平台游戏。在这个游戏中,你必须跳过钉子。所以我创建钉子并将其转换为一个符号,一个电影剪辑。当它注册为电影剪辑时,它不是一个三角形(像钉子一样),而是一个矩形。这意味着当玩家避开钉子并跳跃时,如果他离得太近,他就会死亡,但是他没有击中钉子,而是击中钉子周围不可见的矩形。是否有办法更改电影剪辑的形状,使其仅适合钉子和钉子。您可以使用hittest,下面是一个使用示例 根据我处理同一类型问题的经验,您不能将作为3 hitTestObject(如果您遇到此问题,我可

好的,我在flash中有一个游戏,一个平台游戏。在这个游戏中,你必须跳过钉子。所以我创建钉子并将其转换为一个符号,一个电影剪辑。当它注册为电影剪辑时,它不是一个三角形(像钉子一样),而是一个矩形。这意味着当玩家避开钉子并跳跃时,如果他离得太近,他就会死亡,但是他没有击中钉子,而是击中钉子周围不可见的矩形。是否有办法更改电影剪辑的形状,使其仅适合钉子和钉子。

您可以使用hittest,下面是一个使用示例


根据我处理同一类型问题的经验,您不能将
作为3 hitTestObject
(如果您遇到此问题,我可以想象您正在使用该对象)用于MC的特定形状。它总是默认为占据电影剪辑本身空间的方框

因此,要回答您的问题,不,您不能更改MC的形状使其仅为尖峰,默认情况下,
hitTestObject
使用MC周围的边界框

解决这个问题的方法很少(很多人建议不要使用
hittesojbect
,因为它不是很有效,而是编写自己的命中测试代码。我建议在网上寻找这个例子,有很多)

如果你不想处理这个问题,你的另一个选择是创建一个单独的盒形mc系列,作为你的边界框,可以排列成数组,然后只需运行一个循环,看看你的角色是否击中其中任何一个。这样,您可以根据需要调整边界框的大小

但是如果您想使用
hitTestObject
,很遗憾,您必须使用整个对象边界框。由于我不完全确定您将如何处理这一问题,因此我最好的建议是上网阅读AS3中的命中检测基础知识


您的问题在于命中检测,而不一定是电影剪辑。

对象之间的内部命中测试将检查对象的边界框,因此不适用于您

如果你能以某种方式使用玩家代理作为一个点(最低点,比如他的脚中间或者类似的东西),你可以使用
spike.hitTestPoint(globalFootX,globalFootY,true)

如果这不起作用,您必须手动为项目创建hittest表示,并执行您自己的hittest逻辑

另一种解决方案是将项目绘制到单独的精灵,然后查看像素是否重叠。我知道我是在一个旧的AS2项目中做这件事的,在一个不规则形状的世界里,不规则形状的人工智能机器人四处移动

我将提供这些代码以供参考,它可能可以转换为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;
    }
}
类messer_studios.utils.CollisionDetection{
静态公共函数checkForCollision(p_clip1:MovieClip,p_clip2:MovieClip,p_alphaTolerance:Number):矩形{
//设置默认参数:
如果(p_alphaTolerance==未定义){
p_α公差=255;
}
//获取边界:
var bounds1:Object=p_clip1.getBounds(_root);
var bounds2:Object=p_clip2.getBounds(_root);
//排除任何我们知道不会碰撞的东西:
如果((bounds1.xMax
您可以使用
BitmapData
hitTest
来检查像素级冲突,就像这样

(要测试代码,请在Flash stage上放置两个符号“rectClip”和“spike”。也可以先测试,让它们远离,然后再让它们接触并测试。)

(无论哪种方式,您都可以设置
MouseMove
startDrag()
并实时检查。)

祝你好运

var rect:Rectangle = rectClip.getBounds(this);
var rectClipBmpData = new BitmapData(rect.width, rect.height, true, 0);
rectClipBmpData.draw(rectClip);

var spikeRect:Rectangle = spike.getBounds(this);
var spikeBmpData = new BitmapData(spikeRect.width, spikeRect.height, true, 0);
spikeBmpData.draw(spike);

if(rectClipBmpData.hitTest(new Point(rectClip.x, rectClip.y),
                                 255,
                                 spikeBmpData,
                                 new Point(spike.x,spike.y),
                                 255 ))
{
     trace("hit");
}else
{
     trace("No hit");
}