如何对 AS3 中的两个动态添加的对象进行命中测试?

发布于 2024-11-25 18:22:36 字数 96 浏览 0 评论 0原文

我有一个动态添加到舞台的对象网格,这些对象是红色类或蓝色类,如何对这些类执行命中测试?我希望蓝色物体一旦碰到红色物体就消失。如果您有视频或教程链接,请帮忙,我们将不胜感激。谢谢。

I have a grid of objects that have been added to the stage dynamically, the objects are either of a Class Red or Class Blue, how do I perform a hit test on the Classes? I want the Blue object to disappear once it hits a Red one. Help please, if you have a video or a link to a tutorial that would be appreciated. Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

酒解孤独 2024-12-02 18:22:36

您应该使用碰撞检测套件

http://code.google.com/p/collisiondetectionkit/

You should Use the Collision Detection Kit

http://code.google.com/p/collisiondetectionkit/

沦落红尘 2024-12-02 18:22:36

以下是教程的链接,下面是该教程的摘录,显示了基本原理:

import flash.events.Event;

this.addEventListener( Event.ENTER_FRAME, handleCollision)

function handleCollision( e:Event ):void
{
    if(circle_mc.hitTestObject(rect_mc))
       {
           output_txt.text = "HIT"
       }
       else
       {
           output_txt.text = "MISS"
       }
}

http://www.designscripting.com/2011/05/hittest-as3-for-collision-detection-flash-actionscript-3/

你想要做的是也许将上面的代码嵌入到您的 Blue 类中,然后基本上检查并查看您是否击中了舞台上的任何对象,然后检查以确保其类型为 Red 或您想要的任何类型,然后根据结果执行您想要的操作:

import flash.events.Event;
import mypackage.Red;

this.addEventListener( Event.ENTER_FRAME, handleCollision)

function handleCollision( e:Event ):void
{
    if(stage){
        var i:int = 0;
        for(i; i < stage.numChildren; ++i){
            if(this.hitTestObject(stage.getChildAt(i) && stage.getChildAt(i) != this){
                if(stage.getChildAt(i) is Red){
                   //This object has collided with a Red object
                }
            }
        }
    }        
}

我只是将这些代码从我的脑海中拍下来,但如果它不能“开箱即用”,那么只需使用基本原理并进行相应的修改即可。此外,让许多对象在每一帧上同时扫描舞台的所有子对象最终将是一项真正的密集任务,并且会滞后于您的应用程序。更可取的是在某个地方有一个 Enter_frame 事件,其中有两个嵌套的 for 循环检查所有子项以查看谁与谁碰撞,或者更好的是红/蓝对象的链表结构并使用链表循环来检查碰撞。

Here is a link to a tutorial, and below an excerpt from that tutorial that shows the basic principle:

import flash.events.Event;

this.addEventListener( Event.ENTER_FRAME, handleCollision)

function handleCollision( e:Event ):void
{
    if(circle_mc.hitTestObject(rect_mc))
       {
           output_txt.text = "HIT"
       }
       else
       {
           output_txt.text = "MISS"
       }
}

http://www.designscripting.com/2011/05/hittest-as3-for-collision-detection-flash-actionscript-3/

What you're going to want to do is perhaps embed that code above inside your Blue class, and then basically check and see if you hit any object on the stage, and then check to ensure it's type is type Red or whatever you want, then do whatever you want based on the result:

import flash.events.Event;
import mypackage.Red;

this.addEventListener( Event.ENTER_FRAME, handleCollision)

function handleCollision( e:Event ):void
{
    if(stage){
        var i:int = 0;
        for(i; i < stage.numChildren; ++i){
            if(this.hitTestObject(stage.getChildAt(i) && stage.getChildAt(i) != this){
                if(stage.getChildAt(i) is Red){
                   //This object has collided with a Red object
                }
            }
        }
    }        
}

I just slapped that code together off the top of my head but if it doesn't work "out of the box" then just use the basic principle and modify it accordingly. Also, having many objects all scanning all children of the stage at the same time on every frame will eventually be a real intensive task and lag your app. What would be more preferable is to have a single enter_frame event somewhere with two nested for loops checking all children to see who collides with who, or even better a linked-list structure for Red/Blue objects and use a linked list loop to check collision.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文