2013-07-16 71 views
1

我試圖將一個對象隨機移動到不同的位置,所以我拿出以下內容:transition.to隨機生成x,y以及時間,並在完成時運行另一個函數,檢查對象是否仍在那裏,並將其發送到不同的位置。如何用transition.to隨機移動一個對象?

,但我得到一個錯誤:

Runtime error 
main.lua:352: stack overflow 
stack traceback: 
    main.lua:352: in function 
'toAnotherPlace' 

看起來像電暈並沒有真正等待過渡完整,所以它的推移無限循環

代碼

function toAnotherPlace(object) 
    if object ~= nil then 
     transition.to(object, 
      { 
       time=math.random(1500,6000), 
       alpha=1, 
       x=(math.random(10, 310)), 
       y=(math.random(10, 400)), 
       onComplete=toAnotherPlace(object) 
      }) 
    end 
end 

transition.to(bossess[boss], 
    { 
     time=math.random(1500,6000), 
     alpha=1, 
     x=(math.random(10, 310)), 
     y=(math.random(10, 400)), 
     onComplete=toAnotherPlace(bossess[boss]) 
    }) 

回答

1

你可以試試這個,我添加了一個onComplete = function() ... end,我把它叫做toAnotherPlace(object)函數。

我認爲這是一個錯誤,如果你直接調用onComplete

function toAnotherPlace(object) 
    print(object.width) 
    if object ~= nil then 
     transition.to(object, 
     { 
      time = math.random(1500,6000), 
      alpha = 1, 
      x = math.random(10, 310), 
      y = math.random(10, 400), 
      onComplete = function() 
       toAnotherPlace(object) 
      end 
     }) 
    end 
end 

transition.to(bossess[boss], 
{ 
    time = math.random(1500,6000), 
    alpha = 1, 
    x = math.random(10, 310), 
    y = math.random(10, 400), 
    onComplete = function() 
     toAnotherPlace(bossess[boss]) 
    end 
}) 

我想這樣的功能和工作正常,沒有錯誤。

如果仍然收到錯誤,請檢查bossess[boss]如果有你的對象

+0

是的參考,這是準確的話,謝謝! – Bnhjhvbq7