如何在 Actionscript 3 中获取正在碰撞的对象?

发布于 2024-10-13 00:52:00 字数 365 浏览 5 评论 0原文

有没有办法知道一个对象与哪个对象发生碰撞?...我想在 flash 中创建一个“盒子对象”,它可以识别与其碰撞的任何其他对象(影片剪辑)。例如,如果我将盒子放在“场”(引擎或世界)上,然后将任何东西放入其中,我希望盒子告诉我它正在与哪种物体发生碰撞。我无法使用 hitTestObject,因为我事先不知道盒子会与哪个对象发生碰撞。

我想要实现的粗略伪代码如下:

 if ( Movieclip(parent). UNKNOWN_OBJECT .hitTestObject(this) )
        trace(UNKNOWN_OBJECT.name);

上面示例中的 UNKNOWN_OBJECT 不一定具有相同的数据类型。

Is there a way to know which object an object is colliding with?... I want to create a 'box object' in flash that can identify any other object(Movieclips) that collides with it. For example, if I drop the box on the 'field' (engine or world), and i put anything inside it, I would like the box to tell me what kind of object it is colliding with. I can't use hitTestObject because I don't know which object the box will collide with in advance.

A rough pesudocode to what I want to achieve is as follows:

 if ( Movieclip(parent). UNKNOWN_OBJECT .hitTestObject(this) )
        trace(UNKNOWN_OBJECT.name);

UNKNOWN_OBJECT in the above example doesn't necessarily have the same data type.

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

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

发布评论

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

评论(1

吖咩 2024-10-20 00:52:00

您可以在每一帧迭代父级的所有子级,以查看是否发生任何碰撞。不过,这是一种强力检查,如果您有很多对象要检查碰撞,我建议您查看 四叉树或类似的东西。

以下是“盒子对象”如何检查碰撞的示例:

// this is your ENTER_FRAME handler
private function handleEnterFrame(evt:Event):void {
    var p:MovieClip = parent as MovieClip;
    if(!p){
        return;
    }
    for(var i:int = 0, len:int = p.numChildren; i < len; i++){
        var child:DisplayObject = p.getChildAt(i);
        if(child != this && this.hitTestObject(child)){
            trace("Collides with: " + getQualifiedClassName(p.getChildAt(i)));
        }
    }
}

它所做的只是每帧检查与父节点(即兄弟节点)的所有子节点的碰撞。当检测到碰撞时,它将追踪与之碰撞的项目的类名称。为了使其更有用,最好在检测到碰撞时调度一些事件或信号,以便您的类可以“侦听”碰撞。

You could iterate over all children of the parent at every frame to see if there's any collision occurring. This is a brute-force check though and if you have lots of objects to check collisions against, I suggest you look into Quadtrees or something similar.

Here's an example how your "box object" could check collisions:

// this is your ENTER_FRAME handler
private function handleEnterFrame(evt:Event):void {
    var p:MovieClip = parent as MovieClip;
    if(!p){
        return;
    }
    for(var i:int = 0, len:int = p.numChildren; i < len; i++){
        var child:DisplayObject = p.getChildAt(i);
        if(child != this && this.hitTestObject(child)){
            trace("Collides with: " + getQualifiedClassName(p.getChildAt(i)));
        }
    }
}

All it does is checking collisions with all the child-nodes of the parent (i.e. siblings) every frame. When a collision is detected, it will trace out the class name of the item it collided with. To make this more useful it would be a good idea to dispatch some Event or Signal at the time a collision is detected, so your classes can "listen" for collisions.

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