Question about transition.to And event.target.removeSelf(),

Hello ,sorry my english ,…
When finish the transaction.to I need remove the object MyBox[x], but I have error into function Borrar with event.target.removeSelf(), but if use the event.target:removeSelf() into the function Calavera its OK, but I not see the transition, thanks

[code]
function Borrar (event)
event.target:removeSelf() ERROR-----
end

function Calavera (event)
transition.to( event.target, {time=2500, delay=50 , xScale=40,yScale=40 , onComplete=Borrar } )
–event.target:removeSelf()

end

for nTable=1, 10, 1 do
MyBox[nTable] = display.newImage(“images/Myimage.png”)
MyBox[nTable]:addEventListener(“touch”, Calavera)
end
[/code] [import]uid: 79875 topic_id: 31064 reply_id: 331064[/import]

The Borrar function does not have the same scope as the Calavera function. This means it does not ‘see’ the same tables or objects. Try using a function closure in your transition callback. This method will pass the event.target variable into Borrar.

[lua]function Borrar (object)
object:removeSelf() --Try making this more generic
end

function Calavera (event)
transition.to( event.target, {time=2500, delay=50 , xScale=40,yScale=40 , onComplete=function() Borrar(event.target) end } )
end[/lua] [import]uid: 168249 topic_id: 31064 reply_id: 124209[/import]

Thanks Cooperlabs,
But , This function remove the object immediately, not wait to finish command transation.to, event onCompleted , … :frowning:

[code]
local Borrar = function (obj)
obj:removeSelf() --OK
end

function Calavera (event)
transition.to( event.target, {time=2500, delay=50 , xScale=40,yScale=40 ,onComplete=Borrar(event.target) } )

end

for nTable=1, 10, 1 do
MyBox[nTable] = display.newImage(“images/Myimage.png”)
MyBox[nTable]:addEventListener(“touch”, Calavera)
end
[/code] [import]uid: 79875 topic_id: 31064 reply_id: 124391[/import]

You did not use the function closure from the prior post.

[lua]function Calavera (event)
transition.to( event.target, {time=2500, delay=50 , xScale=40,yScale=40 ,onComplete=function() Borrar(event.target) end } )
end[/lua] [import]uid: 168249 topic_id: 31064 reply_id: 124397[/import]

Hello again, and thanks
I used and tested your solution and others solutions, to finally use this code that its OK,thanks!

The solution in :
Corona display-object-listeners
Corona dremoving-objects-and-variables

Important call function onComplete=MyFunction OR onComplete=MyFunction() the use of parentesis

Note by Tom Newman
Note that I used onComplete=listener1 and not onComplete=listener1(). The difference is you are passing a reference to the listener’s function (address) in the first case, and in the second case, you are calling the listener during initialization and using whatever is returned from the listener as the function to be called when the transition finishes.

[code]
local function removeObject( obj )

display.remove( obj )
obj= nil
end
function Calavera (event)

transition.to( event.target, {time=250, delay=50 , xScale=40,yScale=40 , onComplete=removeObject } )
end

[/code] [import]uid: 79875 topic_id: 31064 reply_id: 124402[/import]

The Borrar function does not have the same scope as the Calavera function. This means it does not ‘see’ the same tables or objects. Try using a function closure in your transition callback. This method will pass the event.target variable into Borrar.

[lua]function Borrar (object)
object:removeSelf() --Try making this more generic
end

function Calavera (event)
transition.to( event.target, {time=2500, delay=50 , xScale=40,yScale=40 , onComplete=function() Borrar(event.target) end } )
end[/lua] [import]uid: 168249 topic_id: 31064 reply_id: 124209[/import]

Thanks Cooperlabs,
But , This function remove the object immediately, not wait to finish command transation.to, event onCompleted , … :frowning:

[code]
local Borrar = function (obj)
obj:removeSelf() --OK
end

function Calavera (event)
transition.to( event.target, {time=2500, delay=50 , xScale=40,yScale=40 ,onComplete=Borrar(event.target) } )

end

for nTable=1, 10, 1 do
MyBox[nTable] = display.newImage(“images/Myimage.png”)
MyBox[nTable]:addEventListener(“touch”, Calavera)
end
[/code] [import]uid: 79875 topic_id: 31064 reply_id: 124391[/import]

You did not use the function closure from the prior post.

[lua]function Calavera (event)
transition.to( event.target, {time=2500, delay=50 , xScale=40,yScale=40 ,onComplete=function() Borrar(event.target) end } )
end[/lua] [import]uid: 168249 topic_id: 31064 reply_id: 124397[/import]

Hello again, and thanks
I used and tested your solution and others solutions, to finally use this code that its OK,thanks!

The solution in :
Corona display-object-listeners
Corona dremoving-objects-and-variables

Important call function onComplete=MyFunction OR onComplete=MyFunction() the use of parentesis

Note by Tom Newman
Note that I used onComplete=listener1 and not onComplete=listener1(). The difference is you are passing a reference to the listener’s function (address) in the first case, and in the second case, you are calling the listener during initialization and using whatever is returned from the listener as the function to be called when the transition finishes.

[code]
local function removeObject( obj )

display.remove( obj )
obj= nil
end
function Calavera (event)

transition.to( event.target, {time=250, delay=50 , xScale=40,yScale=40 , onComplete=removeObject } )
end

[/code] [import]uid: 79875 topic_id: 31064 reply_id: 124402[/import]