我正在学习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函数?
答案 0 :(得分:0)
您在调用中传递字符串文字而不是变量。变化
timer.performWithDelay(100, function() reverseRotate ("rtAm") end, 2*iteration);
到
timer.performWithDelay(100, function() reverseRotate(rtAm) end, 2*iteration);