0

几天前我开始学习LUA,在桌面模拟器游戏中开始了我自己的项目,但是我碰壁了。我无法为等待课程增加时间。

这是我尝试过的一个例子:

function state_check()
    --This function checks if the state of obj_1 and obj_2 is 0 or 1
end

function press_button()
    --This function activates other functions based on the state of obj_1 and obj_2

    i = 1 --Function starts with a wait of 1 second

    if obj_1_state == 1 then --If state is 1, then the function is triggered and 1 second is added to i
        Wait.time(func_1, i)
        i = i + 1
    end

    if obj_2_state == 1 then
        Wait.time(func_2, i)
    end
end

我需要该功能检查第一部分,如果为真,请在 1 秒后执行第二部分。如果没有,请正常执行第二部分并跳过“i = i + 1”。我的问题是该函数同时执行所有操作。我知道我做错了什么,但我不知道是什么。有没有办法创建一些门来按顺序或类似的事情做所有事情?

4

1 回答 1

0

您的代码似乎正确。
我不知道是什么问题。

但我知道一种可能的解决方案是遵循“回调地狱”的编程风格:

local function second_part(obj_2_state)
    if obj_2_state == 1 then
        Wait.time(func_2, 1)
    end
end

local function first_part(obj_1_state, obj_2_state)
    if obj_1_state == 1 then
        Wait.time(
            function() 
                func_1() 
                second_part(obj_2_state)
            end, 1)
    else
        second_part(obj_2_state)
    end
end

function press_button()
    local obj_1_state, obj_2_state = --calculate states of obj_1 and obj_2 here
    first_part(obj_1_state, obj_2_state)
end
于 2020-07-10T21:38:56.207 回答