循环期间Lua更新屏幕

时间:2018-11-29 04:33:16

标签: 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 

在此先感谢您的见解。

1 个答案:

答案 0 :(得分:2)

blog给出了解决此问题的示例。一个有趣的方法是coroutines(或here)方法。这个想法是,您仍然可以像示例中那样编写代码,但是在每次迭代之后,您都会跳出循环,在屏幕上绘制并在您所处的确切位置继续该功能。

可能看起来像这样:

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)