使数字转换顺利进行

时间:2016-03-05 22:59:52

标签: lua

我有一个变量,当调用函数时,它乘以1.01。我想要的是,而不仅仅是看到变量"跳跃"到另一个值,顺利地看到它并逐渐增加,直到达到该值。

示例:

    x = 1000
    function gets called
    x = x * 1.01 
--> x value is now 1010, but when I show it on screen, I would like it to go 1000, 1001, 1002,...,1010 in certain amount of time
--> instead of just 1000 -> 1010

到目前为止我的代码:

function Multiply()
                local random = math.random(1, 102)
                    if random ~= 1 then
                        Multiplier = Multiplier * 1.01
                        MultiplierDisplay.text = "x" .. string.format("%.3f", Multiplier)

                        Gain = Multiplier * PlaceYourBetTextField.text - PlaceYourBetTextField.text
                        GainDisplay.text = "Gain: " .. string.format("%.0f", Gain)
                    else
                        timer.cancel(MultiplyTimer)
                        Multiplier = 1
                    end
                end
                MultiplyTimer = timer.performWithDelay(125, Multiply, 0)

1 个答案:

答案 0 :(得分:0)

您必须选择进行此转换的时间。 我假设这是因为你是成倍增加的东西,你需要一个恒定的几何变化而不是一个恒定的算术变化。

你会做

local x=1000

local x0=0
local x1=0
local time0=0
local time1=0

local function changex(newx,time)
    x0=x
    x1=newx
    time0=os.clock()
    time1=time0+time
end

然后你的主循环:

while true do
    local t=os.clock()
    if t<time0 then
        x=x0
    elseif t<time1 then
        local tratio=(t-time0)/(time1-time0)
        x=x0*(x1/x0)^tratio
    else
        x=x1
    end
    pause()
end