检测 Flash 中的碰撞...或者更好的方法
我正在尝试使用 AS3 在 Flash 中创建一个空气曲棍球游戏。
目前,我正在使用输入框架功能来检查 3 个物体(2 个球拍和一个球)的位置,然后检查它们是否接触,如果接触,则启动检测碰撞功能。
然而,它会在每次加载帧时进行检查,并且在 25 fps 下,这是相当多的,并且应用程序滞后。
有什么想法或更好的方法来做到这一点吗?
提前致谢。
I'm trying to create a air-hockey game, in flash using AS3.
At the moment, I am using an enter frame function to check the positioning of the 3 objects, 2 paddles and a ball, then it checks to see if they are in contact, if they are it then initiates a detect collision function.
However it's checking every time the frame is loaded, and at 25 fps, this is quite a lot and the app lags.
Any ideas, or better ways to do this?
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
两个毕达哥拉斯陈述正在减慢你的游戏速度? 25 帧/秒?出了点问题——不不应该是这样。
完全删除碰撞检测并检查是否恢复了 25fps,然后一次一行添加语句,直到速度再次下降。
检查每帧调用碰撞代码的次数是否超过一次(好吧,两次)。
请记住,您可以在不使用 Math.sqrt 的情况下测试碰撞:(
您仍然需要 sqrt 来解决碰撞,但这应该很少见.)
然而,根据我的经验,尽管 Math.sqrt 是 Pythagoras 最慢的部分,但它仍然足够快以 25fps 管理每帧两次调用。听起来好像还有什么地方不对劲。
Two pythagoras statements are slowing your game down? At 25fps? Something is wrong -- that should not be the case.
Remove the collision detection completely and check that you get your 25fps back, then add statements a line at a time until the slowdown reappears.
Check you are not calling your collision code more than once (well, twice) per frame.
Remember that you can test for collision without using
Math.sqrt
:(You will still need sqrt to resolve the collision but this should be pretty rare.)
However in my experience, even though
Math.sqrt
is the slowest part of Pythagoras, it's still easily fast enough to manage two calls per frame at 25fps. It sounds like something else is wrong.如果您需要定期检查某些内容,我想 EnterFrame 事件是一个合适的机制。
您没有提到是否使用内置的命中测试功能,所以我想我会提到它们: hitTestObject 和 hitTestPoint。
If you need to check something periodically, I guess the enterFrame event is a suitable mechanism to do it.
You don't mention if you use the built-in hit test functions or not, so I thought I'd mention them: hitTestObject and hitTestPoint.
你尝试过定时器吗?
Have you tried a timer?