how to remove object after transition is done

hey, 

let’s say I have an object that spawns on the screen moving upward, I want the object to be removed when object.y = -70

here’s an example for the code I made 

[lua]local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

function scene:createScene(e)
local screenGroup = self.view

function spawnObject ()
local object = display.newImage(“object.png”)
object.x = centerX; object.y = 500;
transition.to(object, {time = 1000 , y = -70}

function removeObjectOntouch (e)
if e.phase == “ended” then
object:removeSelf()

end
end

object:addEventListener(“touch”, removeObjectOntouch)

end

end[/lua]

now this works fine, but what if I want the object to be removed when object.y = -70 ?

what should I add ? I tried many ways to do it but I couldn’t succeed with it.

what do you think I should do ?

Hi @hammod-930,

This is simple… just use a transition “onComplete” listener function, as illustrated in the examples at the bottom of this page:

http://docs.coronalabs.com/daily/api/library/transition/to.html

Brent

Hi Brent, 

after using “onComplete” listener it works only with the second object, and ther first object that spawned hasn’t be removed,

and after that when I use removeObjectOntouch function for  three times there’s an error that says : attempt to call method ‘removeSelf’ (a nil value) stack traceback !

Can I see the code you’re using at this time?

here’s the whole code , I changed object:removeSelf() to "display.remove(object) to avoid the error

here’s the code:

[lua]local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight

local physics = require(“physics”)
physics.start()
–physics.setDrawMode(“hybrid”)
physics.setGravity(0,0)

local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

function scene:createScene(e)
local screenGroup = self.view

function spawnObject ()
local object = display.newImage(“object.png”)
object.x = math.random(100,400); object.y = 500;
transition.to(object, {time = 2500 , y = -60, onComplete = removeObject})

removeObject = function ()
print(“object transition completed”)
display.remove(object)
end

function removeObjectOntouch (e)
if e.phase == “ended” then
object:removeSelf()
end
end

object:addEventListener(“touch”, removeObjectOntouch)

end

tmr1 = timer.performWithDelay(2000, spawnObject, -1);

end

function scene:enterScene(e)
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]

When you create the transition (transition.to at line 16) the table parameter (second one) is evaluated at the time ; it’s onComplete member is set to the value of ‘removeObject’ at the time which is nil. 

Hence you are effectively writing onComplete = nil which obviously won’t work.

try something like this

  1. transition.to(object, {time = 2500 , y = -60, onComplete = function(obj) obj:remove() end })

Hi paulscottrobson

I tried what you said but the result is an error, please try the code to see if you can fix it or not .

My bad. 

This works - creates a circle, sets its alpha to zero, then transitions alpha and moves it, and removes itself incompletion.

local object = display.newCircle(100,100,30) object.alpha = 0 transition.to(object, {time = 2500 , alpha = 1,x = 300,y = 200,onComplete = function(obj) obj:removeSelf() end})

paulscottrobson

well, this is works but when I use the ‘removeObjectOntouch’ function, there’s an error happens.

thank you guys for trying to help me but I found out how to fix the code, I tried to use 'display.remove(obj) and it worked fine:

1-transition.to(object, {time = green.speed, y = -60, onComplete = function(obj) display.remove(obj) end})

this works fine,

I have another question, can I call to functions with ‘onComplete’ ?

Yup.

local function t ()      print("!") end   local n = display.newCircle(0,0,50)   transition.to(n,{x=400, onComplete = t}) -- bear in mind, to pass in parameters you'll need closures, google it.

Hi 

86lemonade68

sorry for the mistake, I meant can I call two functions with ‘onComplete’ ?

Hi @hammod-930,

This is simple… just use a transition “onComplete” listener function, as illustrated in the examples at the bottom of this page:

http://docs.coronalabs.com/daily/api/library/transition/to.html

Brent

Hi Brent, 

after using “onComplete” listener it works only with the second object, and ther first object that spawned hasn’t be removed,

and after that when I use removeObjectOntouch function for  three times there’s an error that says : attempt to call method ‘removeSelf’ (a nil value) stack traceback !

Can I see the code you’re using at this time?

here’s the whole code , I changed object:removeSelf() to "display.remove(object) to avoid the error

here’s the code:

[lua]local centerX = display.contentCenterX
local centerY = display.contentCenterY
local _W = display.contentWidth
local _H = display.contentHeight

local physics = require(“physics”)
physics.start()
–physics.setDrawMode(“hybrid”)
physics.setGravity(0,0)

local storyboard = require (“storyboard”)
local scene = storyboard.newScene()

function scene:createScene(e)
local screenGroup = self.view

function spawnObject ()
local object = display.newImage(“object.png”)
object.x = math.random(100,400); object.y = 500;
transition.to(object, {time = 2500 , y = -60, onComplete = removeObject})

removeObject = function ()
print(“object transition completed”)
display.remove(object)
end

function removeObjectOntouch (e)
if e.phase == “ended” then
object:removeSelf()
end
end

object:addEventListener(“touch”, removeObjectOntouch)

end

tmr1 = timer.performWithDelay(2000, spawnObject, -1);

end

function scene:enterScene(e)
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]

When you create the transition (transition.to at line 16) the table parameter (second one) is evaluated at the time ; it’s onComplete member is set to the value of ‘removeObject’ at the time which is nil. 

Hence you are effectively writing onComplete = nil which obviously won’t work.

try something like this

  1. transition.to(object, {time = 2500 , y = -60, onComplete = function(obj) obj:remove() end })

Hi paulscottrobson

I tried what you said but the result is an error, please try the code to see if you can fix it or not .

My bad. 

This works - creates a circle, sets its alpha to zero, then transitions alpha and moves it, and removes itself incompletion.

local object = display.newCircle(100,100,30) object.alpha = 0 transition.to(object, {time = 2500 , alpha = 1,x = 300,y = 200,onComplete = function(obj) obj:removeSelf() end})

paulscottrobson

well, this is works but when I use the ‘removeObjectOntouch’ function, there’s an error happens.