English 中文(简体)
Actionscript 3.0 Flash中精灵的碰撞检测
原标题:Collision Detection of Sprites in Actionscript 3.0 Flash

我正在AS3.0中制作一个类似于阿东死亡库夫的游戏。到目前为止,我已经完成了4名不同球员的动作,效果不错。

我现在要进行碰撞检测,以测试蠕虫——可以说,是在相互碰撞还是在碰撞自己的尾巴。

据我所知,如果我使用hitTestObject();它将使用整个对象的注册区域,这将是一个巨大的问题,因为这个注册是一个包含所有对象的四边注册。因此,如果使用这种方法,只需进入这个矩形,它就会发生碰撞,而不是碰撞到实际的蠕虫。这一点理解正确吗?

我一直在研究不同的碰撞检测方法,似乎找不到适合我项目的最佳方法。

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

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

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

问候-Jesper

最佳回答

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

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()后,此函数会从两个分别绘制不同颜色的对象的形状中获取快照,然后将它们覆盖在一个新的对象上,使颜色相交,返回结果形状的矩形。太酷了。

要了解有关像素完美碰撞检测的更多信息,您可以在谷歌上搜索碰撞检测,然后输入以下名称之一:“ActionScript Man”、“Troy Gilbert“、”Boulevard(wim)、“
Grant Skinner(gSkinner)Seneyor
。顺便说一句,那些家伙是很棒的AS3参考。

问题回答

你所描述的问题是碰撞检测中非常常见的问题,因为物体有一个设定的宽度和高度,因此将矩形定义为物体。

然而,有一种解决方案可以在像素级上制作碰撞检测系统。我在官方网站上发现了这一点,这使我能够在像素级对位图进行碰撞检测。

http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7d55.html一

希望它能以同样的方式帮助你。

看看那个游戏的屏幕截图,我认为最好的模型是将每个蠕虫描述为一个圆圈链。然后用比圆半径稍大的单元格在网格中划分世界/级别。

碰撞检查将是:

  1. clear grid
  2. place each circle into the 1 or more grid cells it falls in
  3. iterate over all cells, for each cell:
    • for each pair of circles (partially) in this cell, check if they intersect. If they do; collision. Note that this may result in more than 1 collision occurrence between circle A and B, so you d also need to check that to avoid duplicates.

步骤1和2可以通过不清除网格来优化,而不是步骤2,在每个圆移动后更新其单元格。如果将单元格的大小设置为圆形的5倍,则圆形可以在同一单元格中停留几帧,从而避免过多的添加/删除操作。

我现在正在我的一个项目中做类似的事情,除了太空船!我的网格单元目前是256x256(我认为对你的项目来说太大了),我的单元半径约为20。





相关问题
Disable button tooltip in AS3

I want to disable the tooltip on certain buttons. The tooltip manager seems to be an all or nothing solution. Is it possible to disable the tooltip for just one or two buttons?

Sorting twodimensional Array in AS3

So, i have a two-dimensional Array of ID s and vote count - voteArray[i][0] = ID, voteArray[i][1] = vote count I want the top 3 voted items to be displayed in different colors, so i have a 2nd Array -...

Virtual Tour using sketch up, ajax, flash technologies

I want to know if there are existing technology that make your 3d models in sketch into virtual tours, using either Ajax or Flash for web presentation. If there s none, which will be a good approach ...

AS3 try/catch out of memory

I m loading a few huge images on my flex/as3 app, but I can t manage to catch the error when the flash player runs out of memory. Here is the what I was thinking might work (I use ???? because i dont ...

Red5 Security Tutorial

I am looking for a step by step tutorial on securing Red5 from intrusion. This seems to be a question that comes up alot in a google search, but is never really answered in a way that makes sense to ...

Clipboard access using Javascript - sans Flash?

Is there a reliable way to access the client machine s clipboard using Javascript? I continue to run into permissions issues when attempting to do this. How does Google Docs do this? Do they use ...

visible property of DisplayObject

For example I have a hierarchy of movie clips. mc1 is a child of mc, and mc2 is a child of mc1. Turns out that when I set mc1.visible = false; mc2.visible stays true. Is that supposed to happen?...

热门标签