【发布时间】:2013-05-17 15:09:30
【问题描述】:
我正在学习 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 函数?
【问题讨论】:
-
function reverseRotate(rtAm) if rtAm > 0 ...和function()reverseRotate(rtAm)end -
当然我忘了在圆括号中添加 rtAm...还重写了文档中的引号:D 愚蠢的 meeeee。谢谢叶戈尔!
-
@EgorSkriptunoff 请在答案部分发表您的评论。 Neru,一旦有答案,请接受他的回答。
-
@EgorSkriptunoff,停止在 cmets 中回答! ;)