2013-05-18 113 views
2

嘿,我正在使用此代碼來移動(動畫)我的對象在場景 但它泄漏內存並停止響應。如何在corona sdk中移動物體?

//transition back 
local function goBack() 
    transition.to (wall2, { time = 10000, x = 100, y = 310, onComplete = startTransition}) 
    transition.to (wall, { time = 10000, x = 700, y = 200, onComplete = startTransition}) 
    transition.to (gate_a, { time = 10000, x = 100, y = 255, onComplete = startTransition}) 
    transition.to (stargate_a, { time = 10000, x = 100, y = 255, onComplete = startTransition}) 
end 

//transition start 
function startTransition() 
    transition.to (wall2, { time = 10000, x = 700, y = 310, onComplete = goBack}) 
    transition.to (wall, { time = 10000, x = 100, y = 200, onComplete = goBack}) 
    transition.to (gate_a, { time = 10000, x = 700, y = 255, onComplete = goBack}) 
    transition.to (stargate_a, { time =10000, x = 700, y = 255, onComplete = goBack}) 
end 

startTransition() 

如何正確移動對象而不會泄漏任何內存?

回答

8

這樣做:

//transition back 
local function goBack() 
    transition.to (wall2, { time = 10000, x = 100, y = 310}) 
    transition.to (wall, { time = 10000, x = 700, y = 200}) 
    transition.to (gate_a, { time = 10000, x = 100, y = 255}) 
    transition.to (stargate_a, { time = 10000, x = 100, y = 255, onComplete = startTransition}) 
end 

//transition start 
function startTransition() 
    transition.to (wall2, { time = 10000, x = 700, y = 310}) 
    transition.to (wall, { time = 10000, x = 100, y = 200}) 
    transition.to (gate_a, { time = 10000, x = 700, y = 255}) 
    transition.to (stargate_a, { time =10000, x = 700, y = 255, onComplete = goBack}) 
end 

startTransition() 

由於所有的持續時間是一樣的,沒有必要要求onComlpete上的所有過渡。


如果您需要,您可以取消函數內的轉換。爲此,爲轉換指定一個名稱,檢查它是否仍在編程中,然後停止它。我會告訴你一個例子。這不是強制性的,但如果你仍然從記憶喪失執行上面的代碼之後的痛苦,你可以使用它:

local trans_1,trans_2; 
local function goBack() 
    if(trans_1)then transition.cancel(trans_1) end -- to cancel an existing transition 
    trans_2 = transition.to (wall2, { time = 10000, x = 100, y = 310}) 
end 

function startTransition() 
    if(trans_2)then transition.cancel(trans_2) end -- to cancel an existing transition 
    trans_1 = transition.to (wall2, { time = 10000, x = 700, y = 310}) 
end 

startTransition() 

保持編碼.............