2

嘿,我正在使用此代码在场景中移动(动画)我的对象但它会泄漏内存并停止响应。

//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()

如何在不泄漏任何内存的情况下正确移动对象?

4

1 回答 1

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( )

继续编码......

于 2013-05-19T06:37:41.583 回答