通过timer.PerformWithDelay传递参数 - Lua

我正在学习Lua语言,有点困惑。我想通过timer.PerformWithDelay方法将参数传递给函数。下面是我编写的代码:

local function animate ( event )
    gear.rotation = gear.rotation + rotateAmount;
end
Runtime:addEventListener("enterFrame", animate);
------------------------------------------------
function reverseRotate()
    if tonumber(rtAm) > 0 then  -- 此处出现错误:“尝试将数字与nil进行比较”
        rotateAmount = rotateAmount - 1;
    elseif tonumber(rtAm) < 0 then
        rotateAmount = rotateAmount + 1;
    end
end
------------------------------------------------
local buttonHandler = function ( event )
    if event.phase == "ended" then
        local iteration = math.abs(rotateAmount);
            if rotateAmount > 0 then
                local rtAm = rotateAmount;
                timer.performWithDelay(100, function() reverseRotate ("rtAm") end, 2*iteration);
            elseif rotateAmount < 0 then
                local rtAm = rotateAmount;
                timer.performWithDelay(100, function() reverseRotate ("rtAm") end, 2*iteration);
            end
    end
end

我的问题是:为什么变量rtAm没有传递给reverseRotation函数?

点赞
用户1190388
用户1190388

你在函数调用时传递了一个字符串字面量,而不是变量。请将

timer.performWithDelay(100, function() reverseRotate ("rtAm") end, 2*iteration);

改为

timer.performWithDelay(100, function() reverseRotate(rtAm) end, 2*iteration);
2013-05-17 05:50:28