cocos2d 最佳实践,多线程与调度程序
嗯,没有太多的示例代码。我正在编写一个游戏循环,但遇到了障碍。起初,我尝试为循环创建一个时间表,因为这被列为最佳实践中应该完成的方式。我的帧速率是 60 fps,循环也以 60 fps 的间隔运行。然而,我很快注意到间隔时间只不过是一个建议,并且该函数更新的实际速率非常不稳定,范围在 15 到 65 fps 之间。这导致对象更新的跳跃,即使它们的更新距离是作为时间的函数调节的。一旦更新速度低于 20 fps,更新时就会变得非常明显并且非常难看。
然后我尝试创建自己的线程,以便我可以更严格地调节更新率。通过这种方法,我几乎能够精确地调节我的更新速率。这样所有的动作和动画就都很流畅了。这种方法的问题是 cocos2d 显然不支持多线程,因为我定期看到屏幕撕裂。另外,如果我的更新率与我的绘制率不完全匹配,我会看到跳跃。我相信这是由于在更新循环中间触发了绘制方法,这是多线程的常见问题。
我能想到的解决这个问题的两种方法是找到某种方法来严格调节调度程序的触发率,或者找到一种方法来锁定绘图代码直到我可以完成更新。我花了相当长的时间寻找有关如何执行其中任何一个的示例,但没有成功。如果有人对如何处理这种情况有更清楚的了解,我将非常感谢一些建议。我只是太习惯 C++ 了,事情按照我告诉他们的速度发生。如果您需要我发布任何其他信息,请询问!预先感谢您的任何帮助。
Well, there isn't much example code for this. I'm writing a game loop and I'm running into a snag. At first I attempted to create a schedule for the loop as this was listed under best practices as the way it should be done. My frame rate is 60 fps, and the loop was also running at an interval of 60 fps. I quickly noticed however that the interval time is little more than a suggestion, and the actual rate that the function is updating is highly erratic, ranging between 15 and 65 fps. This led to jumps in objects updating, even when their update distance was regulated as a function of time. Once updating dropped below 20 fps it became highly apparent when it updated and was quite ugly.
I then tried to create my own thread so that I could more closely regulate the update rate. With this method I was able to almost precisely regulate my updating rate. With this all movement and animations were smooth. The issue with this method is that cocos2d clearly doesn't support multithreading well as I periodically see screen tearing. Also, if my update rate doesn't precisely match my draw rate I see jumps. I believe this is due to the draw method firing in in the middle of my update loop, a common problem with multithreading.
The two ways I can think of to solve this problem are to find some way to closely regulate the firing rate of the scheduler, or to find a way to lock the drawing code until I can finish my update. I've been looking for quite some time for any examples on how to do either of these, and have met with no success. If anyone out there has a clearer idea of how to handle this kind of a situation I would be very appreciative of some advice. I'm just too used to C++ and having things happen at the rate I tell them to. If you need me to post any additional information, just ask! Thanks in advance for any assistance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你的问题出在你预定的功能上。它只是无法按照您想要的速度运行。尝试从 cocos2d 运行 helloWorld 场景,您会发现 fps 稳定在 60 附近(59.8 - 60.2)
Your problem is in your scheduled function. It just can't run as fast as you want. Try running helloWorld scene from cocos2d and you will notice that fps are stable near 60 (59.8 - 60.2)