【问题标题】:Passing arguments through timer.PerformWithDelay - Lua通过 timer.PerformWithDelay 传递参数 - Lua
【发布时间】: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 &gt; 0 ...function()reverseRotate(rtAm)end
  • 当然我忘了在圆括号中添加 rtAm...还重写了文档中的引号:D 愚蠢的 meeeee。谢谢叶戈尔!
  • @EgorSkriptunoff 请在答案部分发表您的评论。 Neru,一旦有答案,请接受他的回答。
  • @EgorSkriptunoff,停止在 cmets 中回答! ;)

标签: lua coronasdk


【解决方案1】:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-11-26
    • 2021-07-25
    • 2022-01-25
    • 2014-05-22
    • 1970-01-01
    • 1970-01-01
    • 2023-04-08
    • 1970-01-01
    相关资源
    最近更新 更多