如何对 AS3 中的两个动态添加的对象进行命中测试?
我有一个动态添加到舞台的对象网格,这些对象是红色类或蓝色类,如何对这些类执行命中测试?我希望蓝色物体一旦碰到红色物体就消失。如果您有视频或教程链接,请帮忙,我们将不胜感激。谢谢。
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您应该使用碰撞检测套件
http://code.google.com/p/collisiondetectionkit/
You should Use the Collision Detection Kit
http://code.google.com/p/collisiondetectionkit/
以下是教程的链接,下面是该教程的摘录,显示了基本原理:
http://www.designscripting.com/2011/05/hittest-as3-for-collision-detection-flash-actionscript-3/
你想要做的是也许将上面的代码嵌入到您的 Blue 类中,然后基本上检查并查看您是否击中了舞台上的任何对象,然后检查以确保其类型为 Red 或您想要的任何类型,然后根据结果执行您想要的操作:
我只是将这些代码从我的脑海中拍下来,但如果它不能“开箱即用”,那么只需使用基本原理并进行相应的修改即可。此外,让许多对象在每一帧上同时扫描舞台的所有子对象最终将是一项真正的密集任务,并且会滞后于您的应用程序。更可取的是在某个地方有一个 Enter_frame 事件,其中有两个嵌套的 for 循环检查所有子项以查看谁与谁碰撞,或者更好的是红/蓝对象的链表结构并使用链表循环来检查碰撞。
Here is a link to a tutorial, and below an excerpt from that tutorial that shows the basic principle:
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:
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.
http://www.foundation-flash.com/tutorials/as3hittesting/
http://www.foundation-flash.com/tutorials/as3hittesting/