Hi,
I have spawned object that uses the same transition. but when the object is removed the transition is still working because of the onComplet has a print and the print comes on console even when the object is removed.
Hi,
I have spawned object that uses the same transition. but when the object is removed the transition is still working because of the onComplet has a print and the print comes on console even when the object is removed.
Please show your code. You may have a hanging reference to the object, in which case the Lua table will stay alive, but the display object itself will be gone.
or, add this line to the beginning of your onComplete method:
if(obj.removeSelf == nil) then return end
Change obj to whatever name you’re using for your object in the arguments to the listener (self, obj, enemy, player, etc.)
Oh, and what did that line do? It looked at the object and checked for a field named ‘removeSelf’, which is a standard function/method for all display objects. When a display object is destroyed, the ‘removeSelf’ method is stripped off and the reference is nil-ed (effectively).
Or you can save the reference to the transition as the object’s property and cancel it when the object gets removed.
[lua]
local img = display.newImageRect(…)
img.t = transition.to(img, {time = 250, y = 100, onComplete = function()
print(“completed”)
end})
–when you need to remove img
transition.cancel(img.t) --transition will stop, onComplete won’t be called
img:removeSelf()
img = nil
[/lua]
I have many objects in my game and a lot of them have transitions. Each object stores a table of transitions and when it’s time for an object to get removed, it loops through its transitions, stops each of them and after that gets removed.
this is my code :
[lua]
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
function scene:createScene(e)
local screenGroup = self.view
function spawnObject ()
object = display.newImage(“object.png”)
–object.alpha = 0.9
object.x = math.random(100,300); object.y = 500;
tr1 = transition.to(object, {time = 2500, y = -50, onComplete = function(obj) display.remove(obj) counter() end})
function counter()
c = c+1;
if (c == 3) then
print(“you lose”)
end
end
function removeObjectOntouch (e)
if e.phase == “began” then
if e.target ~= nil then
e.target:removeSelf()
end
end
end
object:addEventListener(“touch”, removeObjectOntouch)
end
end
function scene:enterScene(e)
tmr1 = timer.performWithDelay(1000, spawnObject, -1);
end
function scene:exitScene(e)
end
function scene:destroyScene(e)
end
scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)
return scene [/lua]
I’ll try the codes above and let you guys know
I thought this problem will never be solved, I thank you very much for your help
this works, but makes the game lags horribly ! which means that after the object is gone the transition will continue but without the onComplete method, it’s 50% solution, haw can I fix this if possible ?
Please show your code. You may have a hanging reference to the object, in which case the Lua table will stay alive, but the display object itself will be gone.
or, add this line to the beginning of your onComplete method:
if(obj.removeSelf == nil) then return end
Change obj to whatever name you’re using for your object in the arguments to the listener (self, obj, enemy, player, etc.)
Oh, and what did that line do? It looked at the object and checked for a field named ‘removeSelf’, which is a standard function/method for all display objects. When a display object is destroyed, the ‘removeSelf’ method is stripped off and the reference is nil-ed (effectively).
Or you can save the reference to the transition as the object’s property and cancel it when the object gets removed.
[lua]
local img = display.newImageRect(…)
img.t = transition.to(img, {time = 250, y = 100, onComplete = function()
print(“completed”)
end})
–when you need to remove img
transition.cancel(img.t) --transition will stop, onComplete won’t be called
img:removeSelf()
img = nil
[/lua]
I have many objects in my game and a lot of them have transitions. Each object stores a table of transitions and when it’s time for an object to get removed, it loops through its transitions, stops each of them and after that gets removed.
this is my code :
[lua]
local storyboard = require (“storyboard”)
local scene = storyboard.newScene()
function scene:createScene(e)
local screenGroup = self.view
function spawnObject ()
object = display.newImage(“object.png”)
–object.alpha = 0.9
object.x = math.random(100,300); object.y = 500;
tr1 = transition.to(object, {time = 2500, y = -50, onComplete = function(obj) display.remove(obj) counter() end})
function counter()
c = c+1;
if (c == 3) then
print(“you lose”)
end
end
function removeObjectOntouch (e)
if e.phase == “began” then
if e.target ~= nil then
e.target:removeSelf()
end
end
end
object:addEventListener(“touch”, removeObjectOntouch)
end
end
function scene:enterScene(e)
tmr1 = timer.performWithDelay(1000, spawnObject, -1);
end
function scene:exitScene(e)
end
function scene:destroyScene(e)
end
scene:addEventListener(“createScene”, scene)
scene:addEventListener(“enterScene”, scene)
scene:addEventListener(“exitScene”, scene)
scene:addEventListener(“destroyScene”, scene)
return scene [/lua]
I’ll try the codes above and let you guys know
I thought this problem will never be solved, I thank you very much for your help
this works, but makes the game lags horribly ! which means that after the object is gone the transition will continue but without the onComplete method, it’s 50% solution, haw can I fix this if possible ?