由洪水填充引起的 StackOverflow 错误:如何停止错误?
对于我的游戏中的关卡编辑器,我有一个洪水填充功能。该地图的大小为 3600 个图块(60 x 60),我从洪水填充函数(因为它调用自身)收到堆栈溢出错误。
如果我只填充较小的半径,它就可以正常工作。如何阻止错误发生?
或者,有没有办法告诉闪存运行时清除堆栈,因为不需要返回到函数?
For my level editor in my game, I have a floodfill function. The map has a size of 3600 tiles (60 by 60), and I get a stack overflow error from my floodfill function (as it calls itself).
If I only floodfill a smaller radius, it works fine. How do I stop the error from occuring?
Alternatively, is there a way to tell the flash runtime to clear the stack as there is no need to return back to the function?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要制作一个不依赖于递归的洪水填充函数,因为 Flash 的堆栈非常有限。您可以在维基百科上找到一些非递归方法: http://en.wikipedia.org/wiki/ Flood_fill#Alternative_implementations
You need to make a flood fill function that doesn't rely on recursion because Flash's stack is quite limited. You can find some non-recursive ways on Wikipedia: http://en.wikipedia.org/wiki/Flood_fill#Alternative_implementations
我猜你可能正在使用洪水填充的简单递归实现;该算法可以创建深堆栈。您可以实现一种更高效的基于队列的算法并保存堆栈,但我建议使用
BitmapData
对象已内置的洪水填充功能。BitmapData
由 Flash Player 提供,具有相当快且堆栈友好的洪水填充实现。不久前我正在研究洪水填充算法,最终使用内置 API 主要是因为速度优势——以及它是编译的 C 代码与 ActionScript 的比较。
以下是文档:
flash.display.BitmapDataFloodFill()
I'm guessing you might be using a simple recursive implementation of flood-fill; that algorithm can create deep stacks. It's possible you could implement one of the more efficient queue based algorithms and save the stack, however I would suggest using the already built-in flood-fill capabilities of the
BitmapData
object.BitmapData
is provided by the Flash Player and has a pretty fast and stack friendly flood-fill implementation.I was playing with flood-fill algorithms a while back and ended up using the built-in APIs mainly because of the speed advantage -- as well it's compiled C code vs ActionScript.
Here are the docs:
flash.display.BitmapData floodFill()