Problems with Composer Scene Transitions

I am making a game in Corona and I want to publish it, so it needs to look good, but for some reason, when I use composer.gotoScene, my transitions don’t show up. Please Help

Here is my code

(title.lua)

local composer = require("composer")
local widget = require("widget")

local scene = composer.newScene()
local stage = display.currentStage

local function StartButtonEvent(event)

if event.phase == 'ended' then
	while stage.numChildren > 0 do
		local obj = stage[1]
		obj:removeSelf()
		obj = nil
	end
	composer.gotoScene("template", {effect = "fade", time = 500})
	
end

end

local start_btn = widget.newButton({
width = 50,
height = 25,
left = display.contentCenterX,
top = display.contentCenterY,
label = "START",
labelSize = 100,
onEvent = StartButtonEvent
})

return scene

(template.lua)

local composer = require( “composer” )

local scene = composer.newScene()


– Code outside of the scene event functions below will only be executed ONCE unless
– the scene is removed entirely (not recycled) via “composer.removeScene()”



– Scene event functions


– create()
function scene:create( event )

local sceneGroup = self.view

local ball = display.newCircle(100,100,100,100)

sceneGroup:insert(ball)

end

– show()
function scene:show( event )

local sceneGroup = self.view
local phase = event.phase

if ( phase == "will" ) then
  
  

elseif ( phase == "did" ) then
    -- Code here runs when the scene is entirely on screen

end

end

– hide()
function scene:hide( event )

local sceneGroup = self.view
local phase = event.phase

if ( phase == "will" ) then
    -- Code here runs when the scene is on screen (but is about to go off screen)

elseif ( phase == "did" ) then
    -- Code here runs immediately after the scene goes entirely off screen

end

end

– destroy()
function scene:destroy( event )

local sceneGroup = self.view
-- Code here runs prior to the removal of scene's view

end


– Scene event function listeners


scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene

Thank you in advance!

I think you’re using the composer badly.
If you want to use it you have to do it correctly using the scene object and its methods.
For example you could write title.lua, like this:

local composer = require("composer")
local widget = require("widget")

local scene = composer.newScene()
local stage = display.currentStage

local start_btn

local function StartButtonEvent(event)
    composer.gotoScene("template")    
end

function scene:create( event )
    print("create")
    local sceneGroup = self.view
    start_btn = widget.newButton(
    {
        width = 50,
        height = 25,
        x = display.contentCenterX,
        y = display.contentCenterY,
        label = "START",
        labelSize = 100,
        onPress = StartButtonEvent
    })
    sceneGroup:insert(start_btn)
end

function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase
    
    if phase == "will" then
    elseif phase == "did" then
    end 
end

function scene:hide( event )
    local sceneGroup = self.view
    local phase = event.phase
    
    if event.phase == "will" then
    elseif phase == "did" then
    end 
end

function scene:destroy( event )

    local sceneGroup = self.view
    start_btn:removeSelf()
    start_btn = nil
end


scene:addEventListener( "create", scene )
scene:addEventListener( "show", scene )
scene:addEventListener( "hide", scene )
scene:addEventListener( "destroy", scene )


return scene

Sorry, but when I insert the start_btn button inside sceneGroup, I understand that the associated memory will be automatically released by sceneGroup.

Usually, if I don’t have special needs, I declare start_btn as local:

   local sceneGroup = self.view
   local start_btn = widget.newButton (
     {

I have never had problems, or strange behavior. It’s wrong ?

Renato

I’m not sure why, but also in the template that corona sdk creates you as a new game it says explicitly:
“widgets must be manually removed”, I suppose having an associated listener (the function you pass to the onPress parameter) remains a “reference” and therefore it is better to delete it by hand.

Could you give me the reference?

I studied the example in dir:

/ Applications / CoronaSDK / SampreCode / Interface / WidgetDemo

does not even bother to close the scenes it open on Tab1, Tab2, Tab2, Tab4 and are full of widget .

Tanks!

Renato

The tab widget demo does not remove the widgets from the scene, it does a simple show / hide
This also makes sense in a businness app, as switching between tabs often makes no sense to destroy and rebuild widgets every time.
Different in a game, during the “play” scene you need the maximum memory for the game objects and you don’t want to keep references and memory occupied for widgets in title or end game screens.

Thank you, it’s true! I have to do more test about it.
If I try something similar on my scene

start_btn:removeSelf()

I receive an immediate crash!

Renato

Try display.remove(start_btn)

That won’t crash even if start_btn is already removed and nil.

1 Like

I suggest using display.remove(obj) instead of obj:removeSelf(). It is safer.

:slight_smile: Looks like @kbradford and I were reading and responding at the same time. Nice!

2 Likes