Search code examples
luagideros

attempt to perform arithmetic on a table value: Lua error message


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)

Basicallu, what I am trying to do is, change the speed of rotation randomly, but since I do not want it to change at every second, I tried using Timer.delayedCall in Gideros, but it gives an error that says attempt to perform arithmetic on a table value: Lua error message. How can I fix this?


Solution

  • According to Gideros documentation, Timer.delayedCall returns a 'Timer' object, which should be the table the error message is referring to. http://docs.giderosmobile.com/reference/gideros/Timer/delayedCall

    I'm not intimately familiar with Gideros but I believe you would want something closer to this:

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

    However, this will presumably still fire with every ENTER_FRAME event, just that each change will be delayed randomly. You may want to use a control variable so that only one Timer can be pending:

    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)