如何在不终止函数的情况下返回值?

时间:2017-03-11 13:30:46

标签: lua corona

这里它返回0.2,因为它在不等待计时器迭代的情况下停止了函数。

function Ani.Speed(maxSpeed, phase)
    if(phase == accelerate) then   
        function speedUp()
            if(maxSpeed ~= 0) then
                maxSpeed = 0
            end
            returnedSpeed = maxSpeed + 0.2
            return(returnedSpeed)
        end
        timer.performWithDelay(1000, speedUp, 10)
    end

end

如何在每次迭代时返回maxSpeed?

预期结果:

print(maxSpeed) -- 0/0.2/0.4/0.6/(...)/2, where "/" is interval in second

1 个答案:

答案 0 :(得分:0)

在timer.performWithDelay之前声明一个本地数组变量,并将所有maxSpeed添加到speedUp函数内的数组中。在timer.performWithDelay之后,返回数组。

根据要求,请在下面找到建议的代码:

if(phase == accelerate) then
    returnedSpeed = []
    i = 0 #(array index)
    function speedUp()
        if(maxSpeed ~= 0) then
            maxSpeed = 0
        end
        returnedSpeed[i] = maxSpeed + 0.2
        i = i + 1
    end
    timer.performWithDelay(1000, speedUp, 10)
    return(returnedSpeed) 
end