Ber*_*eri 2 iphone android ios coronasdk
嘿,我正在使用此代码移动(动画)我的对象在场景但它泄漏内存并停止响应.
//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()
Run Code Online (Sandbox Code Playgroud)
如何在不泄漏任何内存的情况下正确移动对象?
这样做:
//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()
Run Code Online (Sandbox Code Playgroud)
由于所有持续时间都相同,因此无需在所有转换上调用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( )
Run Code Online (Sandbox Code Playgroud)
继续编码.............