Actionscript 3.0 Flash 中精灵的碰撞检测 [英] Collision Detection of Sprites in Actionscript 3.0 Flash

查看:29
本文介绍了Actionscript 3.0 Flash 中精灵的碰撞检测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在 AS3.0 中制作类似 achtung die kurve 的游戏.到目前为止,我已经完成了 4 个不同玩家的动作,效果还不错.

I am making an achtung die kurve-like game in AS3.0. So far I've done the movements of the 4 different players, and it works alright.

我现在要进行碰撞检测,以测试蠕虫"——可以这么说,是与彼此或它自己的尾巴发生碰撞.

I am now to make collision detection, in order to test if a 'worm'-so to speak, is colliding with eachother or its own tail.

据我所知,如果我使用 hitTestObject();它将使用整个对象的注册区域,这将是一个巨大的问题,因为此注册进行了包含所有对象的 4 面注册.所以如果使用它,它会通过输入这个矩形而不是击中实际的蠕虫来碰撞".这是否正确理解?

As I understand it, if I use hitTestObject(); it will use the registration area of the whole object, which would be a huge problem, seeing since this registration makes a 4-sided registration that contains all of the object. So if this is used, it will 'collide' just by entering this rectangle instead of hitting the actual worm. Is this correctly understood?

我一直在寻找不同的碰撞检测方法,但似乎无法找到最适合我的项目的方法.

I've been looking through different methods of collision detection, and can't seem to find an optimal one for my project.

我的想法是检查蠕虫"是否在白色背景上绘制了它们的新精灵.如果不是,那么它一定是撞到了什么东西.

My thought were to check if the 'worms' are drawing their new sprites on a white background. if they aren't, then it must have hit something.

您可以在此处查看我如何使用我的代码:链接到 .fla 文件的 .as 格式代码

You can see how I used my code here: code in .as format linked to an .fla file

抱歉我的问题表述不当,希望它有点道理.任何帮助是极大的赞赏!!

Sorry for my ill-formulated question, hope it makes somewhat sense. Any help is greatly appreciated!!

最好的问候 - Jesper

Best regards - Jesper

推荐答案

如果您想要具有高效 CPU 使用率的 像素完美碰撞检测,请尝试此功能:

Try this function if you want a Pixel Perfect Collision Detection with efficient CPU usage:

trace("Collided: " + (areaOfCollision(mc1, mc2) != null));
trace("Where: " + areaOfCollision(mc1, mc2));

function areaOfCollision(object1:DisplayObject, object2:DisplayObject, tolerance:int = 255):Rectangle {
    if (object1.hitTestObject(object2)) {
        var limits1:Rectangle = object1.getBounds(object1.parent);
        var limits2:Rectangle = object2.getBounds(object2.parent);
        var limits:Rectangle = limits1.intersection(limits2);
        limits.x = Math.floor(limits.x);
        limits.y = Math.floor(limits.y);
        limits.width = Math.ceil(limits.width);
        limits.height = Math.ceil(limits.height);
        if (limits.width < 1 || limits.height < 1) return null;

        var image:BitmapData = new BitmapData(limits.width, limits.height, false);
        var matrix:Matrix = object1.transform.concatenatedMatrix;
        matrix.translate(-limits.left, -limits.top);
        image.draw(object1, matrix, new ColorTransform(1, 1, 1, 1, 255, -255, -255, tolerance));
        matrix = object2.transform.concatenatedMatrix;
        matrix.translate(-limits.left, -limits.top);
        image.draw(object2, matrix, new ColorTransform(1, 1, 1, 1, 255, 255, 255, tolerance), BlendMode.DIFFERENCE);

        var intersection:Rectangle = image.getColorBoundsRect(0xFFFFFFFF, 0xFF00FFFF);
        if (intersection.width == 0) return null;
        intersection.offset(limits.left, limits.top);
        return intersection;
    }
    return null;
}

在成功的初步hitTestObject()之后,这个函数在后台从两个分别涂有不同颜色的对象的形状中获取快照,然后将它们叠加在一个新的颜色上,返回<结果形状的代码>矩形.太酷了.

After a successful preliminary hitTestObject(), this function backgroundly takes a snapshot from the shapes of both objects painted with different colors each, then overlays them intersecting the colors on a new one, returning the Rectangle of the resulting shape. So cool.

要了解有关 Pixel Perfect Collision Detection 的更多信息,您可以在 google Collision Detection 后跟以下名称之一:The ActionScript Man",Troy Gilbert"、Boulevart (wim)"、Grant Skinner (gSkinner)"或Sencular".顺便说一下,这些家伙是很棒的 AS3 参考资料.

To learn more about Pixel Perfect Collision Detection you can google Collision Detection followed by one of these names: "The ActionScript Man", "Troy Gilbert", "Boulevart (wim)", "Grant Skinner (gSkinner)" or "Senocular". Those guys are awesome AS3 references by the way.

这篇关于Actionscript 3.0 Flash 中精灵的碰撞检测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆