Lua更新循环期间屏幕

我正在编写一个函数,使屏幕上的角色沿着标记的路径行走。我想遍历该角色的所有标记,并根据每个标记更新显示。现在发生的是,显示仅在迭代结束时更新一次。根据一些常见问题解答,似乎Lua是按照这种方式设计的。那么在Lua中实现逐渐移动的最佳方式是什么?

local function follow_movement_path (moving_char)
    these_markers = moving_char.move_markers
    for m, n in ipairs(these_markers) do
        this_marker = n
        moving_char.x = this_marker.x
        moving_char.y = this_marker.y
        print(this_marker.current_space.name)
        sleep(1)
    end
end

非常感谢您的任何见解。

点赞
用户8572843
用户8572843

这个 博客 给出了一个解决这个问题的例子。一个有趣的方法是 coroutines(或者这里)。思路是你仍然可以像你的例子一样编写代码,但在每次迭代之后,您会跳出循环,在屏幕上绘制并在离开的位置继续执行函数。

可能是这样的:

local function follow_movement_path (moving_char)
    these_markers = moving_char.move_markers
    for m, n in ipairs(these_markers) do
        this_marker = n
        moving_char.x = this_marker.x
        moving_char.y = this_marker.y
        print(this_marker.current_space.name)
        coroutine.yield()
    end
end

local c = coroutine.create(follow_movement_path)
coroutine.resume(c)
draw_on_display()
coroutine.resume(c)
2018-11-29 07:20:57