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

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

问题描述

我想提出一个ACHTUNG模具kurve类游戏的AS3.0。到目前为止,我已经做了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.

我现在做碰撞检测,以测试是否worm',可以这么说,是碰撞海誓山盟或自己的尾巴。

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.

您可以看到我是如何用我的code在这里: code在。至于形式链接到的.fla文件

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

对不起,我的虐待制定的问题,希望这使得有些道理。 任何帮助是极大AP preciated!

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

最好的问候 - 加斯帕

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;
}

在一个成功的preliminary hitTestObject(),此功能backgroundly需要从两个物体涂上不同颜色的每个形状的快照,然后覆盖它们相交的颜色上一个新的,返回矩形所产生的形状。太酷了。

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.

要了解更多关于像素完美碰撞检测您可以在google 冲突检测接着其中一个名称: ActionScript的人特洛伊吉尔伯特 Boulevart(WIM)格兰特斯金纳(gSkinner) Senocular 。那些家伙的方式真棒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天全站免登陆