Does removeSelf() triggers finalize()?

Hi, i have a problem with my hero:removeSelf() for some reason it doesn’t trigger my function Hero:finalize(). I put my hero:removeSelf() in scene:hide()

function scene:hide( event )
    if ( event.phase == "will" ) then
        hero:removeSelf()
        --world = nil
    elseif ( event.phase == "did" ) then
        Runtime:removeEventListener("enterFrame", enterFrame)
    end
end

and this is my finalize

function Hero:finalize()
        print("hilang kan")
		Hero:removeEventListener( "collision" )
		Runtime:removeEventListener( "enterFrame", enterFrame )
		Runtime:removeEventListener( "key", key )
    end

Couple of quick questions:

Did you declare Hero and finalize outside of the scene:create function? If they are local to the scene:create or scene:show function, they will be invisible to scene:hide. Because removeSelf() checks to make sure the object isn’t a nil value before attempting to remove it, it could very well be that Hero is nil for scene:hide, but you’re not getting an error message because removeSelf() is just ignoring it.

Did you attach an event listener to Hero?
Hero:addEventListener( "finalize" )

Also, the finalize function should be an event:
function Hero:finalize( event )

Man, i’ve been searching for hours. And the problem is only because i forgot too add the listener XD. Thankyou for the reply!

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.