0

I've got an algorithm that takes more than several seconds to complete. I'm trying to implement it into a 60fps flash game i'm creating. I am wondering if there is some provision in ActionScript3 to interrupt a computation in order to update the frame and continue the computation afterwards. There probably isn't, so I'm assuming the best method would be to perform the computation for x milliseconds, measure what the frame rate is after, then adjust the time the computation will run for next frame if the frame rate is less than or greater than 60 fps. The drawback with this is that the game won't run at a steady 60fps...

Any other ideas on how to (optimally) perform a large computation in AS3 while maintaining the framerate?

EDIT: For the curious (the calculation, perhaps I should have said algorithm): I'm creating a motion planning library for game AI objects (one use for motion planning). The algorithm is along the lines of a RRT (rapidly exploring random tree) that iterates thousands of times.

Demo link: http://www.swfcabin.com/open/1279044355

Click to motion plan. The circle can "thrust" at a fixed magnitude in any direction.

Also - there is no linear velocity damping (i.e. friction).

4

3 回答 3

2

你可以分阶段进行“计算”,然后每帧只做一个阶段,直到完成?将它分成你知道可以放入框架的块,并随着时间的推移进行。

几个例子:

我从事的一款游戏是计算实体之间的视线。花了很长时间,硬件不是很强大,所以我们每帧只更新一个对象的视线。玩家的视线每隔一帧更新一次,而敌人在中间帧中循环更新。

相反,如果您基于大型数据集计算某些内容,则将该数据划分为较小的块,并且每帧只执行一个块。例如,如果您需要找出离您的船最近的恒星,并且有一百万颗恒星,那么每帧可能只计算一千颗。

也许您的计算不容易映射到其中任何一个;如果您有一个示例说明您要完成的工作,那将非常有帮助!

于 2010-07-12T20:53:03.590 回答
2

根据您的计算类型,您可以编写一个 Pixel Bender 内核,使用 ShaderJob 在后台进行处理:

http://www.boostworthy.com/blog/?p=243

http://www.huesforalice.com/project/47

或者,这里是如何在 Actionscript 中实现伪线程的教程的另一个链接:http: //blogs.adobe.com/aharui/2008/01/threads_in_actionscript_3.html

于 2010-07-13T13:40:57.753 回答
0

更新:flash player自 11.4 以来一直支持 工人

后台工作人员允许在后台执行任务而不会阻塞似乎非常适合您的需求的主要工作人员。作为一个不错的奖励,在多核 cpu 上,后台工作人员将能够并行运行而不会减慢主工作人员的速度

AIR mobile 目前不支持 iO 上的工作人员(AIR 4.0 和 AIR 13 beta)。

另外,正如其他人已经说过的,寻路不应该那么长!您应该考虑使用另一种算法,因为Rapid听起来与需要几秒钟才能完成的算法不兼容。最著名的寻路算法是A 星 (A*)

于 2014-03-19T15:00:57.857 回答