Im doing something wrong..

Hey!

Im a new at both corona and programming in general - but LOVING it! I have spent ALOT of hours learning from tuts and forums and can do some nice lines of code :) 

BUT - i have a problem! I realised this problem when going back and forth between two scenes in an app i made which made the program crasch! 

so i did a test app. just two (well three if you count main) scenes with two buttons, platBtn and a backBtn. Spent Alot of days trying to figure out how to propperly nil out everything.

The result?

   The.Same.Craching.Program!!

The code::::

*********************************************************************************************************************************

   main.lua

local composer = require(“composer”)

composer.gotoScene(“menu”)

*********************************************************************************************************************************

*********************************************************************************************************************************

  menu.lua

----// Composer thingy

local composer = require( “composer” )

local scene = composer.newScene()


– All code outside of the listener functions will only be executed ONCE unless “composer.removeScene()” is called.


----// in order to make the listener work!

local mpp

---------------------------------------------------------------------------------------------------- “scene:create()”

function scene:create( event )

    local sceneGroup = self.view

----// just the PLAY btn

mpp = display.newImageRect( “mainmenu_play_pressed.png”, 195, 46 )

mpp.x = display.contentCenterX

mpp.y = display.contentCenterY

sceneGroup:insert(mpp)

end

----------------------------------------------------------------------------------------------------  “scene:show()”

function scene:show( event )

    local sceneGroup = self.view

    local phase = event.phase

    if ( phase == “will” ) then

    elseif ( phase == “did” ) then

----// Play btn pressed

local function playPressed (event)

if event.phase == “began” then

composer.gotoScene(“settings”)

return true

end

end

----// and the play listener

mpp:addEventListener ( “touch”, playPressed )

    end

end

----------------------------------------------------------------------------------------------------  “scene:hide()”

function scene:hide( event )

    local sceneGroup = self.view

    local phase = event.phase

    if ( phase == “will” ) then

    elseif ( phase == “did” ) then

    end

end

------------------------------------------------------------------------------------------------------ “scene:destroy()”

function scene:destroy( event )

    local sceneGroup = self.view

----// trying to remove obj:listener

mpp:removeEventListener ( “touch”, playPressed )

----// trying to remove the play btn from mem

display.remove( mpp )

mpp = nil

----// Not sure, need to remove composer require from mem?

composer = nil

----// tryin to nil functions…

playPressed = nil

end

------------------------------------------------------------------------------------------------------ Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )


return scene

*********************************************************************************************************************************

*********************************************************************************************************************************

   settings.lua

----// Composer thingy

local composer = require( “composer” )

local scene = composer.newScene()


– All code outside of the listener functions will only be executed ONCE unless “composer.removeScene()” is called.


----// in order to make the listener work!

local backbutton2

-------------------------------------------------------------------------------------------------------- “scene:create()”

function scene:create( event )

    local sceneGroup = self.view

----// just the BACK btn

backbutton2 = display.newImageRect( “backbutton.png”, 51, 48 )

backbutton2.x = display.contentCenterX

backbutton2.y = display.contentCenterY

sceneGroup:insert(backbutton2)

end

-------------------------------------------------------------------------------------------------------- “scene:show()”

function scene:show( event )

    local sceneGroup = self.view

    local phase = event.phase

    if ( phase == “will” ) then

    elseif ( phase == “did” ) then

----// Back btn pressed 

local function back (event)

if event.phase == “began” then

composer.gotoScene(“menu”)

return true

end

end

----// back btn pressed: eventListener

backbutton2:addEventListener ( “touch”, back )

    end

end

-------------------------------------------------------------------------------------------------------- “scene:hide()”

function scene:hide( event )

    local sceneGroup = self.view

    local phase = event.phase

    if ( phase == “will” ) then

   

    elseif ( phase == “did” ) then

    end

end

-------------------------------------------------------------------------------------------------------- “scene:destroy()”

function scene:destroy( event )

    local sceneGroup = self.view

----// trying to remove obj:listener

backbutton2:removeEventListener ( “touch”, back )

----// trying to remove the back btn from mem

display.remove( backbutton2 )

backbutton2=nil

----// Not sure, need to remove composer require from mem?

composer = nil

----// tryin to nil functions…

back = nil

end

-------------------------------------------------------------------------------------------------------- Listener setup

scene:addEventListener( “create”, scene )

scene:addEventListener( “show”, scene )

scene:addEventListener( “hide”, scene )

scene:addEventListener( “destroy”, scene )

return scene

***********************************************************************************************

There are the three files: main, menu and settings and if you try to run the program and repeatedly press the btns everything will crach!

Obviously you need to include a backbutton.png and a mainmenu_play_pressed.png

Questions: 

   How do i fix this and whats going wrong?

* When using a memory function memory goes up to 4000KB + and just freezes :slight_smile:

* Using the composer template.

Some comments:

  1. Post your code in code tags otherwise it’s very hard to read the code.

  2. You create the event listeners in the scene:show function which means that they will be created each time the scene is opened. The correct way is to create them in the scene:create function which will only be called once.

  3. There is no need to delete your object manually if they have been inserted into the sceneGroup, composer will handle it manually.

I cleaned up your code a little bit to make it easier to understand and this should work now:

main.lua

local composer = require("composer") composer.gotoScene("menu")

menu.lua

----// Composer thingy local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- ----// in order to make the listener work! local mpp local function onPlay( event ) if event.phase == "began" then composer.gotoScene("settings") return true end end ---------------------------------------------------------------------------------------------------- "scene:create()" function scene:create( event ) print("menu.scene:create(): I only gets called once :(") local sceneGroup = self.view ----// just the PLAY btn mpp = display.newRect( 100, 100, 100, 100 ) mpp.x = display.contentCenterX mpp.y = display.contentCenterY mpp:addEventListener ( "touch", onPlay ) sceneGroup:insert(mpp) end ---------------------------------------------------------------------------------------------------- "scene:show()" function scene:show( event ) print("menu.scene:show(): Look, I get called each time!") local sceneGroup = self.view end ---------------------------------------------------------------------------------------------------- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view end ------------------------------------------------------------------------------------------------------ "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view end ------------------------------------------------------------------------------------------------------ Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

settings.lua

----// Composer thingy local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- ----// in order to make the listener work! local backbutton2 local function onBack( event ) if event.phase == "began" then composer.gotoScene("menu") return true end end -------------------------------------------------------------------------------------------------------- "scene:create()" function scene:create( event ) print("settings.scene:create(): I only gets called once :(") local sceneGroup = self.view backbutton2 = display.newRect( 100, 100, 200, 200 ) backbutton2.x = display.contentCenterX backbutton2.y = display.contentCenterY backbutton2:addEventListener ( "touch", onBack ) sceneGroup:insert(backbutton2) end -------------------------------------------------------------------------------------------------------- "scene:show()" function scene:show( event ) print("settings.scene:show(): Look, I get called each time!") local sceneGroup = self.view end -------------------------------------------------------------------------------------------------------- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view end -------------------------------------------------------------------------------------------------------- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view end -------------------------------------------------------------------------------------------------------- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Best regards,

Tomas

Note: The print statements in scene:show will be printed twice because I don’t check if the phase is “will” or “did”.

Best regards,

Tomas

Thanks alot for your help Tomas, really appreciate it! 

Some comments:

  1. Post your code in code tags otherwise it’s very hard to read the code.

  2. You create the event listeners in the scene:show function which means that they will be created each time the scene is opened. The correct way is to create them in the scene:create function which will only be called once.

  3. There is no need to delete your object manually if they have been inserted into the sceneGroup, composer will handle it manually.

I cleaned up your code a little bit to make it easier to understand and this should work now:

main.lua

local composer = require("composer") composer.gotoScene("menu")

menu.lua

----// Composer thingy local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- ----// in order to make the listener work! local mpp local function onPlay( event ) if event.phase == "began" then composer.gotoScene("settings") return true end end ---------------------------------------------------------------------------------------------------- "scene:create()" function scene:create( event ) print("menu.scene:create(): I only gets called once :(") local sceneGroup = self.view ----// just the PLAY btn mpp = display.newRect( 100, 100, 100, 100 ) mpp.x = display.contentCenterX mpp.y = display.contentCenterY mpp:addEventListener ( "touch", onPlay ) sceneGroup:insert(mpp) end ---------------------------------------------------------------------------------------------------- "scene:show()" function scene:show( event ) print("menu.scene:show(): Look, I get called each time!") local sceneGroup = self.view end ---------------------------------------------------------------------------------------------------- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view end ------------------------------------------------------------------------------------------------------ "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view end ------------------------------------------------------------------------------------------------------ Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

settings.lua

----// Composer thingy local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- ----// in order to make the listener work! local backbutton2 local function onBack( event ) if event.phase == "began" then composer.gotoScene("menu") return true end end -------------------------------------------------------------------------------------------------------- "scene:create()" function scene:create( event ) print("settings.scene:create(): I only gets called once :(") local sceneGroup = self.view backbutton2 = display.newRect( 100, 100, 200, 200 ) backbutton2.x = display.contentCenterX backbutton2.y = display.contentCenterY backbutton2:addEventListener ( "touch", onBack ) sceneGroup:insert(backbutton2) end -------------------------------------------------------------------------------------------------------- "scene:show()" function scene:show( event ) print("settings.scene:show(): Look, I get called each time!") local sceneGroup = self.view end -------------------------------------------------------------------------------------------------------- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view end -------------------------------------------------------------------------------------------------------- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view end -------------------------------------------------------------------------------------------------------- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Best regards,

Tomas

Note: The print statements in scene:show will be printed twice because I don’t check if the phase is “will” or “did”.

Best regards,

Tomas

Thanks alot for your help Tomas, really appreciate it!