1

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

local function animate ( event )
    gear.rotation = gear.rotation + rotateAmount;
end
Runtime:addEventListener("enterFrame", animate);
------------------------------------------------
function reverseRotate()
    if tonumber(rtAm) > 0 then  -- here appear error: "Attempt to compare number with 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 函数?

4

1 回答 1

0

您在调用中传递字符串文字而不是变量。改变

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

timer.performWithDelay(100, function() reverseRotate(rtAm) end, 2*iteration);
于 2013-05-17T05:50:28.617 回答