尝试对表值执行算术运算:Lua错误消息。

stage:addEventListener(Event.ENTER_FRAME,
function()
    Graphic:setRotation(Graphic:getRotation() + Timer.delayedCall(math.random(4, 8), 
        function()
            speed = math.random(1, 30)
            return speed
        end)
    )
end)

我想做的是随机改变旋转的速度,但是我不希望它每秒都改变,所以我在 Gideros 中尝试使用 Timer.delayedCall,但是它会显示一个错误,错误消息为 attempt to perform arithmetic on a table value: Lua error message。如何修复它?

点赞
用户3585949
用户3585949

根据 Gideros 文档,Timer.delayedCall 返回一个“Timer”对象,这可能是错误消息所指的表。 http://docs.giderosmobile.com/reference/gideros/Timer/delayedCall

我不是非常熟悉 Gideros,但我认为你可能需要像这样的代码:

stage:addEventListener(Event.ENTER_FRAME,
    function()
        Timer.delayedCall(math.random(4,8),
            function()
                Graphic:setRotation( Graphic:getRotation() + math.random(1,30) )
            end)
    end)

但是,这个代码似乎仍然会在每个 ENTER_FRAME 事件中触发,只是每个更改都会有随机的延迟。您可能想使用控制变量,以便只能有一个计时器处于等待状态:

local timerPending=false
stage:addEventListener(Event.ENTER_FRAME,
    function()
        if timerPending then return end
        timerPending=true
        Timer.delayedCall(math.random(4,8),
            function()
                Graphic:setRotation( Graphic:getRotation() + math.random(1,30) )
                timerPending=false
            end)
    end)
2014-05-04 15:43:25