【问题标题】:attempt to perform arithmetic on a table value: Lua error message尝试对表值执行算术:Lua 错误消息
【发布时间】:2014-05-04 23:53:42
【问题描述】:
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,我想做的是随机改变旋转速度,但由于我不希望它每秒都改变,我尝试在 Gideros 中使用 Timer.delayedCall,但它给出了一个错误,上面写着 @ 987654322@。我该如何解决这个问题?

【问题讨论】:

    标签: lua gideros


    【解决方案1】:

    根据 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 事件触发,只是每个更改都会随机延迟。您可能希望使用一个控制变量,以便只有一个 Timer 可以挂起:

    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)
    

    【讨论】:

    • 我明白了你的意思,但你不认为 timerpending 只会添加额外的条件检查,因为上面的代码会随机延迟旋转,或者我可能无法理解 timerpending 的精细目的?
    猜你喜欢
    • 2017-04-06
    • 1970-01-01
    • 1970-01-01
    • 2012-01-27
    • 1970-01-01
    • 2014-02-17
    • 2020-08-22
    • 2015-11-07
    • 2014-02-06
    相关资源
    最近更新 更多