Transition problem

Hi

Why object removed befor transition is completed ?

Thank you

_local square = display.newRect( 0, 0, 100, 100 )

local function RemoveObject(event)  
    event:removeSelf()
    event = nil  
end

local function Trans()  
    transition.to ( square, {time = 2000, x = 200, y = 200, onComplete = RemoveObject(square)} )
end         

square:addEventListener(“tap”, Trans)_

A very subtle but important change needs to be made:

local square = display.newRect( 0, 0, 100, 100 )

local function RemoveObject( target )  
    target:removeSelf()
    target = nil  
end

local function Trans()  
    transition.to ( square, {time = 2000, x = 200, y = 200, onComplete = RemoveObjec t} )
end         

square:addEventListener(“tap”, Trans)

When you have onComplete = someFunction( ) with the parens attached, you are calling that function and taking the value returned by it and assigning it to onComplete.  As your code is originally written, it calls RemoveObject() which removes the object and since your function doesn’t return anything, it assigns a nil to onComplete. 

The version I posed above, RemoveObject the function isn’t called, but instead the address of RemoveObject is assigned to onComplete for when the transition finishes, it has a function to run at that time.

Hi guys.  I would suggest shortening this:

transition.to ( square, {time = 2000, x = 200, y = 200, onComplete = RemoveObject} )

to this:

transition.to ( square, {time = 2000, x = 200, y = 200, onComplete = display.remove} )

It’s safer and quicker to code

That worked. Thank you.

A very subtle but important change needs to be made:

local square = display.newRect( 0, 0, 100, 100 )

local function RemoveObject( target )  
    target:removeSelf()
    target = nil  
end

local function Trans()  
    transition.to ( square, {time = 2000, x = 200, y = 200, onComplete = RemoveObjec t} )
end         

square:addEventListener(“tap”, Trans)

When you have onComplete = someFunction( ) with the parens attached, you are calling that function and taking the value returned by it and assigning it to onComplete.  As your code is originally written, it calls RemoveObject() which removes the object and since your function doesn’t return anything, it assigns a nil to onComplete. 

The version I posed above, RemoveObject the function isn’t called, but instead the address of RemoveObject is assigned to onComplete for when the transition finishes, it has a function to run at that time.

Hi guys.  I would suggest shortening this:

transition.to ( square, {time = 2000, x = 200, y = 200, onComplete = RemoveObject} )

to this:

transition.to ( square, {time = 2000, x = 200, y = 200, onComplete = display.remove} )

It’s safer and quicker to code

That worked. Thank you.