2014-05-04 52 views
1
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,我所試圖做的是,隨意改變轉速,但因爲我不希望它改變在每一秒,我想使用Timer.delayedCall在Gideros,但它給了,說attempt to perform arithmetic on a table value: Lua error message錯誤。我怎樣才能解決這個問題?試圖對錶值進行算術:Lua的錯誤信息

回答

2

根據Gideros文檔,Timer.delayedCall返回一個「定時器」對象,它應該是錯誤消息被參考該表。 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) 
+0

非常感謝。有效!萬分感謝! –

+0

我得到了你的觀點,但你不覺得timerpending只會增加一個額外的條件檢查,因爲上面的代碼延遲旋轉隨機或可能是我不能夠理解timerpending的罰款目的是什麼? –