如何在 Actionscript 3 中获取正在碰撞的对象?
有没有办法知道一个对象与哪个对象发生碰撞?...我想在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以在每一帧迭代父级的所有子级,以查看是否发生任何碰撞。不过,这是一种强力检查,如果您有很多对象要检查碰撞,我建议您查看 四叉树或类似的东西。
以下是“盒子对象”如何检查碰撞的示例:
它所做的只是每帧检查与父节点(即兄弟节点)的所有子节点的碰撞。当检测到碰撞时,它将追踪与之碰撞的项目的类名称。为了使其更有用,最好在检测到碰撞时调度一些事件或信号,以便您的类可以“侦听”碰撞。
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:
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.