Help with understanding "function scene:create( event )"

Hey everyone hope all is well.  I am having some problems with my game and am pretty sure it is because I am not understanding the scene create function.  Appreciate any and all help.

How my game is coded is pretty much all images and well almost all my code is written in scene create.  All my images are made in one table such as images.button1 images.button2 etc…  I am rewriting my code (using scene groups instead of handling images one by one).  I used to destroy all my images (:removeSelf then make it nil).  I have not been able to find a proper way to destroy these images when transitioning to a new screen (composer.gotoScene).  Heres the problem, I have around 6 scene groups, play button screen, shop screen, etc…  When the game is loaded only the fundamental images are seen, all other images are loaded in the correct x,y spot but I add the Device Height to the y spot.  Then when they are called I use a transition to bring the scene group up to the screen (- device height on the y spot).  So now im in screen 1, I then go to screen 2, when I go back to screen 1 I click the shop button but the shop screen does not transition up.  I know that the function is called by a print statement but seems the image doesnt exist?  Whats really really weird is that I have a music button and that works when going back to screen 1.  Seems that its deleting all images that are not seen in the phone’s screen when coming back to screen 1.

Now the logistical part tells me that its because i never properly destroyed the images in screen 1 when going to screen 2.  When I go back to screen 1 I am probably just looking at the initial screen 1 that never got destroyed.

Lastly whenever I call functions in, lets say, scene:show it does not find the function if I make the function in scene:create.  

I can defintely supply code if needed but pretty sure its a fundamental thing im not understanding.  As always any help is greatly and highly appreciated.

Very difficult to help without seeing any code…

Fair enough.  I can put the like 450 lines of code but something tells me you just want the fundamentals :slight_smile: reducing it now.

This should just about do it.  Still around 200 lines but most is just comments or needed for composer.  But its working and extremely minimal while still showing the fundamentals of what im doing. 

-- ============================================================= -- mainMenu -- ============================================================= local composer = require( "composer" ) local scene = composer.newScene() ---------------------------------------------------------------------- -- REQUIRES -- ---------------------------------------------------------------------- local jsonFunct = require( "modAll" ) ---------------------------------------------------------------------- -- LOCALS -- ---------------------------------------------------------------------- -- JSON Variables local gameValues = jsonFunct.readGameValues() local storeValues = jsonFunct.readStoreValues() --Assigned JSON Variables local coins = gameValues[1] local lives = gameValues[2] -- Display Variables local dW = display.contentWidth local dH = display.contentHeight local cX = display.contentCenterX local cY = display.contentCenterY local images = {} local screenName = "none" local handleTransitions local sceneName -- Forward Declarations local goToLevelChooser ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view ---------------------------------------------------------------------- -- CREATE IMAGES -- ---------------------------------------------------------------------- --Create Image Groups bgGroup = display.newGroup() sceneGroup:insert( bgGroup ) difficultyChooserGroup = display.newGroup() sceneGroup:insert( difficultyChooserGroup ) --Function to Create Images local function createImage(dispGroup, imageLoc, xSize, ySize, xLoc, yLoc) local i = display.newImageRect(dispGroup, imageLoc, xSize, ySize ) i.x = xLoc i.y = yLoc return i end --Create Background Group images.background = createImage(bgGroup, "mainBackground.png", dW\*3, dH, cX, cY) images.bottomCake = createImage(bgGroup, "mainBottom.png", dW\*.75, dW\*.22, cX, dH\*.85) images.candyWord = createImage(bgGroup, "candy.png", dW\*.456, dW \*0.19, cX, dH\*0.26) images.blueCandy = createImage(bgGroup, "mainMiddle2.png", dW\*.2, dW\*.2, dW\*.43, dH\*.49) images.pinkCandy = createImage(bgGroup, "mainMiddle.png", dW\*.2, dW\*.2, dW\*.54, dH\*.47) images.wordsWord = createImage(bgGroup, "words.png", dW\*.456, dW\*.19, cX,dH\*.53 ) images.playButton = createImage(bgGroup, "playButton.png", dW\*.15, dW\*.15, cX,dH\*.8 ) images.menuButton = createImage(bgGroup, "settingsButton.png", dW\*.11, dW\*.11, (cX-(dW\*.17)), dH\*.83 ) images.storeButton = createImage(bgGroup, "purchaseButton.png", dW\*.11, dW\*.11, (cX+(dW\*.17)), dH\*.83 ) --Difficulty Chooser (Play Button) Group images.backWindowDifficultyChooser = display.newRect(difficultyChooserGroup, cX, cY+(dH\*1.5), dW\*2, dH\*2) images.backWindowDifficultyChooser.fill = {.13, .92} images.difficultyChooserPauseWindow = createImage(difficultyChooserGroup, "pauseWindow.png", dW\*.82, dH, cX, cY+dH) images.difficultyChooserExitButton = createImage(difficultyChooserGroup, "exit.png", dW\*.08, dW\*.08, dW\*.84, dH\*.23+dH) images.difficultyWord = createImage(difficultyChooserGroup, "difficulty.png", dW\*.35, dW\*.092, cX, dH\*.16+dH) images.hardChooser = createImage(difficultyChooserGroup, "difficultyChooser3.png", dW\*.18, dH\*.49, dW\*.73, dH\*.61+dH) images.mediumChooser = createImage(difficultyChooserGroup, "difficultyChooser2.png", dW\*.18, dH\*.49, dW\*.5, dH\*.61+dH) images.easyChooser = createImage(difficultyChooserGroup, "difficultyChooser1.png", dW\*.18, dH\*.49, dW\*.27, dH\*.61+dH) ---------------------------------------------------------------------- -- Transition Handlers -- ---------------------------------------------------------------------- --Transition Screens Down local function transitionExitWindows(event) local phase = event.phase if ( phase == "ended" ) then if screenName=="difficultyChooser" then handleTransitions = transition.to(difficultyChooserGroup, {time=450, y=dH+dH, transition=easing.inBack} ) end screenName="none" end return true end --Transition Windows To Screen local function transitionToScreen(event) local target = event.target local phase = event.phase if ( phase == "ended") then if target==images.playButton and screenName=="none" then screenName="difficultyChooser" handleTransitions = transition.to(difficultyChooserGroup, {time=450, y=-dH, transition=easing.outBack} ) end end return true end ---------------------------------------------------------------------- -- Change Screen -- ---------------------------------------------------------------------- local function changeScene(event) local target = event.target local phase = event.phase if ( phase=="began" ) then audio.stop(2) audio.play(clickSound, {channel=2} ) elseif ( phase == "ended" and screenName=="difficultyChooser") then if target==images.easyChooser then sceneName="easyLevelChooser" elseif target==images.mediumChooser then sceneName="mediumLevelChooser" elseif target==images.hardChooser then sceneName="hardLevelChooser" end handleTransitions = transition.to(difficultyChooserGroup, {time=450, y=dH+dH, transition=easing.inBack, onComplete=goToLevelChooser} ) end return true end ---------------------------------------------------------------------- -- Create Event Listeners -- ---------------------------------------------------------------------- images.playButton:addEventListener( "touch", transitionToScreen )-- Play Button images.easyChooser:addEventListener( "touch", changeScene )-- Hard Level Chooser images.mediumChooser:addEventListener( "touch", changeScene )-- Hard Level Chooser images.hardChooser:addEventListener( "touch", changeScene )-- Hard Level Chooser end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:willEnter( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:didEnter( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:willExit( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:didExit( event ) local sceneGroup = self.view audio.dispose( clickSound ) clickSound = nil audio.dispose( freeGiftSound ) freeGiftSound = nil end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:destroy( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- -- FUNCTION/CALLBACK DEFINITIONS -- ---------------------------------------------------------------------- goToLevelChooser = function ( self, event ) local options = { effect = "fromBottom", time = 750, } composer.gotoScene( sceneName, options ) return true end --------------------------------------------------------------------------------- -- Scene Dispatch Events, Etc. --------------------------------------------------------------------------------- function scene:show( event ) local sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willEnter( event ) elseif( willDid == "did" ) then self:didEnter( event ) end end function scene:hide( event ) local sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willExit( event ) elseif( willDid == "did" ) then self:didExit( event ) end end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

Post was way too hard to read and seemed to include too many questions.  

Post with the idea that readers will be TL;DR types and you’ll get better answers.

Guessing Primary Question…

My guess is this post essentially boils down to, “My scene doesn’t re-draw when I re-enter as I expect it to.”

How I Make - Redraw on each entry scenes

If I am going to re-draw a scene on each entry, I:

  • do not use create() or destroy()
  • create all content in “will” phase of show() listener
  • destroy all content in “did” phase of hide() listener

Alternate

Others will say (I like my way, but this is cool too), that using create() is the way to go.  If you want to do that, then:

Your Code

Is ‘images’ a global?

If you don’t need to touch those objects outside the scene, then ‘images’ should be a local defined at the top of the file.

Simply set it to {} in create (first)

And clear it in destroy by assigning nil.

Did I Misunderstand?

If I’m way off base, please post below with a super short summary of the problem. A great format to follow is (literally as a bullet list):

  • what I did
  • what I saw
  • what I expected to see
  • why I think it is wrong

PS - Don’t forget, composer.* has a wonderful guide that really gives all the details on how it works, when listeners are called, and in general how best to use it:

https://docs.coronalabs.com/guide/system/composer/index.html

Yes this was asked pretty stupid could have asked much simpler and no you nailed it that is exactly what I was asking.  But what im trying that you suggested is giving me the same results.  I tried two main things:

  1. create all content in “will” phase of show() listener and then destroy all content in “did” phase of hide() listener.  Just copy and pasted all the content that was in create into will phase and added the code below.

  2. kept everything in scene:create then made images local (100% right that it did not need to be global, almost all the variables could be local now that I look at it) then made images=nil in destroy like below.

function scene:destroy( event )

  local sceneGroup = self.view

  images=nil

end

Also of course thanks for all the help.  I was just looking at the composer link you gave.  I read this thing at least 15 times 6 months ago when i started coding and just could not get it at all.  Now im looking at it like the holy grail ive been looking for!!  Thanks a ton.

Don’t overthink this.
 
This is the easiest way to create and later destroy all content:

local content -- variable to hold all content (child groups) function scene:show( event ) if( event.phase == "will" ) then content = display.newGroup() self.view:insert( content ) -- local bgGroup = display.newGroup() content:insert( bgGroup ) local difficultyChooserGroup = display.newGroup() content:insert( difficultyChooserGroup ) ... more groups as needed ... skipping definition of createImage -- No need to track handles unless you are using them createImage(bgGroup, "mainBackground.png", dW\*3, dH, cX, cY) createImage(bgGroup, "mainBottom.png", dW\*.75, dW\*.22, cX, dH\*.85) createImage(bgGroup, "candy.png", dW\*.456, dW \*0.19, cX, dH\*0.26) ... skipping rest of creation steps end end function scene:hide( event ) if( event.phase == "did" ) then display.remove( content ) content = nil end end

Prefer Create
If you prefer to use create() and destroy()

local content -- variable to hold all content (child groups) function scene:create( event ) content = display.newGroup() self.view:insert( content ) -- local bgGroup = display.newGroup() content:insert( bgGroup ) local difficultyChooserGroup = display.newGroup() content:insert( difficultyChooserGroup ) ... more groups as needed ... skipping definition of createImage -- No need to track handles unless you are using them createImage(bgGroup, "mainBackground.png", dW\*3, dH, cX, cY) createImage(bgGroup, "mainBottom.png", dW\*.75, dW\*.22, cX, dH\*.85) createImage(bgGroup, "candy.png", dW\*.456, dW \*0.19, cX, dH\*0.26) ... skipping rest of creation steps end function scene:hide( event ) if( event.phase == "did" ) then if( content ) then timer.performWithDelay( 1, function() composer.removeScene( "nameOfSceneHere" ) end ) end end end function scene:destroy( event ) content = nil end

Side note: I think I said this in another post, but all those weird sizes I see have me worried.
 
Ex: dW*.22

I think you are going to be both surprised and very unhappy with the end result when you run this on a few different devices.

That said, maybe (hopefully) I’m wrong.  :unsure:

I say this because that kind of math generally results in a bunch of sub-pixel rounding issues that cause gaps, overlaps, and/or alignment issues.

Really that easy huh.  So I understand that instead of destroying images you destroy the scene groups those images are in, cool.  Will do some testing and update although im pretty sure you solved it.  So in general I dont prefer any type over any other type, I prefer whichever is the most efficient.  If both are equal then I prefer not using create as create is pretty new to me :slight_smile: .  In all honesty, I took most of the way im doing this code from that folder of examples that you sent me around 3 weeks ago.  I am probably not following correct but im pretty sure your doing everything in create in those examples. 

I use these image sizes so that it can scale to any device.  I havent had problems with other devices so far.  If the best way is to use integer values then of course I switch all the code to this :slight_smile: .  In my eyes if I dont scale everything by device height and width then im relying on Corona to handle this which im sure is good but isnt what im doing make it exactly the definition of scalable to any device as im using actual height and width to decide the size of images?  Can I accomplish both the scalability that im saying and the problems you are saying by just making the number round to an integer or is my way of scaling just absolutely ridiculous and wrong haha?

In short, “No.”  Your scaling calculations are not the same as what Corona does.

However, both can result in unwanted artifacts and alignment issues.  So, if edge alignments are critical you have to account for that and:

  • Use a pixel-perfect config.lua solution
  • Use blending modes that don’t cause unwanted artifacts.
  • test, test … test 

You have to ask yourself,

  • What will this look like if my calculation produces a sub-pixel result?
  • Will the images all still line up?
  • Will there be gaps?
  • Will there be overlaps where I don’t want them?

How many devices have you tested on and what devices?  Once you test on a few devices with different resolution I suspect you’ll see what I’m getting at.

Note: At the end of the day none of this matters.  You should, at this point, simply be focused on learning. 

Hopefully you’re not intending to make a game for sale/money, but just to learn. 

In that case, even if the end result has imperfections it will have served the larger purpose.  A learning experience.

I defintely dont think I am doing what Corona does to scale images im sure Corona is much more complex.  I kinda thought what I was doing was making the size / placement universal on any device like this, I have a bit to learn. I have gone through every device in the corona simulator and physical devices, note 4, galaxy s6, galaxy s7, iphone x and iphone 6.  I have a pretty extensive config.lua file I found that I admit is making a lot of this work.  I mean really in the end I only faced problems with iOSx.  Edge alignments are critical in one place but I basically say line up image 2 right where image 1 ends and this way its always lined up.  I will do my rewrite using integers instead of these calculations and let Corona do its thing :slight_smile: .

I did release my app with no intentions and still no intentions of making money :slight_smile: .  I am an IT manager at a start up and want to transition to the programming side.  Programming alone was a little confusing at not knowing where to go and what to study.  I found with Corona I gained a much higher interest in programming as I could interact with apps and a community environment.  I put my app with ads and IAP really to just show I could do it.  The rewrite idea is to write one game somewhat efficiently then next games take concepts from there and try making a more complex situation (next game I want to use physics module extensively).  Putting the game out I have been told looks good on my cv that’s why I go for a whole build out with somewhat decent looking graphics.

In the end all the programmers I work with yell at me for studying LUA haha.  I have started to learn java, python and some mid level algorithims for interview questions through Udemy courses but am still addicted to programming using Corona as I feel I learn programming concepts better here.  I have a long way to go but the interest is there and ive been devoted for around 6-8 months to learn without going back to school, hopefully, one day developer can be my title :slight_smile: .  As always thanks for the help and time you put into your answers.

Note: I wasn’t saying make a change.  Just warning you it might not look right to do your calculations that way.
 
Don’t change it if it is working for you.   Finish instead, then work on a new project and consider changing your approach then.
 
This way you have more fun and get more things made.
 
Cheers,
Ed

Thanks.  I just wanted to add that I am no longer using create and tried the first example of the two examples and all is working and destroying correctly.

Very difficult to help without seeing any code…

Fair enough.  I can put the like 450 lines of code but something tells me you just want the fundamentals :slight_smile: reducing it now.

This should just about do it.  Still around 200 lines but most is just comments or needed for composer.  But its working and extremely minimal while still showing the fundamentals of what im doing. 

-- ============================================================= -- mainMenu -- ============================================================= local composer = require( "composer" ) local scene = composer.newScene() ---------------------------------------------------------------------- -- REQUIRES -- ---------------------------------------------------------------------- local jsonFunct = require( "modAll" ) ---------------------------------------------------------------------- -- LOCALS -- ---------------------------------------------------------------------- -- JSON Variables local gameValues = jsonFunct.readGameValues() local storeValues = jsonFunct.readStoreValues() --Assigned JSON Variables local coins = gameValues[1] local lives = gameValues[2] -- Display Variables local dW = display.contentWidth local dH = display.contentHeight local cX = display.contentCenterX local cY = display.contentCenterY local images = {} local screenName = "none" local handleTransitions local sceneName -- Forward Declarations local goToLevelChooser ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:create( event ) local sceneGroup = self.view ---------------------------------------------------------------------- -- CREATE IMAGES -- ---------------------------------------------------------------------- --Create Image Groups bgGroup = display.newGroup() sceneGroup:insert( bgGroup ) difficultyChooserGroup = display.newGroup() sceneGroup:insert( difficultyChooserGroup ) --Function to Create Images local function createImage(dispGroup, imageLoc, xSize, ySize, xLoc, yLoc) local i = display.newImageRect(dispGroup, imageLoc, xSize, ySize ) i.x = xLoc i.y = yLoc return i end --Create Background Group images.background = createImage(bgGroup, "mainBackground.png", dW\*3, dH, cX, cY) images.bottomCake = createImage(bgGroup, "mainBottom.png", dW\*.75, dW\*.22, cX, dH\*.85) images.candyWord = createImage(bgGroup, "candy.png", dW\*.456, dW \*0.19, cX, dH\*0.26) images.blueCandy = createImage(bgGroup, "mainMiddle2.png", dW\*.2, dW\*.2, dW\*.43, dH\*.49) images.pinkCandy = createImage(bgGroup, "mainMiddle.png", dW\*.2, dW\*.2, dW\*.54, dH\*.47) images.wordsWord = createImage(bgGroup, "words.png", dW\*.456, dW\*.19, cX,dH\*.53 ) images.playButton = createImage(bgGroup, "playButton.png", dW\*.15, dW\*.15, cX,dH\*.8 ) images.menuButton = createImage(bgGroup, "settingsButton.png", dW\*.11, dW\*.11, (cX-(dW\*.17)), dH\*.83 ) images.storeButton = createImage(bgGroup, "purchaseButton.png", dW\*.11, dW\*.11, (cX+(dW\*.17)), dH\*.83 ) --Difficulty Chooser (Play Button) Group images.backWindowDifficultyChooser = display.newRect(difficultyChooserGroup, cX, cY+(dH\*1.5), dW\*2, dH\*2) images.backWindowDifficultyChooser.fill = {.13, .92} images.difficultyChooserPauseWindow = createImage(difficultyChooserGroup, "pauseWindow.png", dW\*.82, dH, cX, cY+dH) images.difficultyChooserExitButton = createImage(difficultyChooserGroup, "exit.png", dW\*.08, dW\*.08, dW\*.84, dH\*.23+dH) images.difficultyWord = createImage(difficultyChooserGroup, "difficulty.png", dW\*.35, dW\*.092, cX, dH\*.16+dH) images.hardChooser = createImage(difficultyChooserGroup, "difficultyChooser3.png", dW\*.18, dH\*.49, dW\*.73, dH\*.61+dH) images.mediumChooser = createImage(difficultyChooserGroup, "difficultyChooser2.png", dW\*.18, dH\*.49, dW\*.5, dH\*.61+dH) images.easyChooser = createImage(difficultyChooserGroup, "difficultyChooser1.png", dW\*.18, dH\*.49, dW\*.27, dH\*.61+dH) ---------------------------------------------------------------------- -- Transition Handlers -- ---------------------------------------------------------------------- --Transition Screens Down local function transitionExitWindows(event) local phase = event.phase if ( phase == "ended" ) then if screenName=="difficultyChooser" then handleTransitions = transition.to(difficultyChooserGroup, {time=450, y=dH+dH, transition=easing.inBack} ) end screenName="none" end return true end --Transition Windows To Screen local function transitionToScreen(event) local target = event.target local phase = event.phase if ( phase == "ended") then if target==images.playButton and screenName=="none" then screenName="difficultyChooser" handleTransitions = transition.to(difficultyChooserGroup, {time=450, y=-dH, transition=easing.outBack} ) end end return true end ---------------------------------------------------------------------- -- Change Screen -- ---------------------------------------------------------------------- local function changeScene(event) local target = event.target local phase = event.phase if ( phase=="began" ) then audio.stop(2) audio.play(clickSound, {channel=2} ) elseif ( phase == "ended" and screenName=="difficultyChooser") then if target==images.easyChooser then sceneName="easyLevelChooser" elseif target==images.mediumChooser then sceneName="mediumLevelChooser" elseif target==images.hardChooser then sceneName="hardLevelChooser" end handleTransitions = transition.to(difficultyChooserGroup, {time=450, y=dH+dH, transition=easing.inBack, onComplete=goToLevelChooser} ) end return true end ---------------------------------------------------------------------- -- Create Event Listeners -- ---------------------------------------------------------------------- images.playButton:addEventListener( "touch", transitionToScreen )-- Play Button images.easyChooser:addEventListener( "touch", changeScene )-- Hard Level Chooser images.mediumChooser:addEventListener( "touch", changeScene )-- Hard Level Chooser images.hardChooser:addEventListener( "touch", changeScene )-- Hard Level Chooser end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:willEnter( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:didEnter( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:willExit( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:didExit( event ) local sceneGroup = self.view audio.dispose( clickSound ) clickSound = nil audio.dispose( freeGiftSound ) freeGiftSound = nil end ---------------------------------------------------------------------- ---------------------------------------------------------------------- function scene:destroy( event ) local sceneGroup = self.view end ---------------------------------------------------------------------- -- FUNCTION/CALLBACK DEFINITIONS -- ---------------------------------------------------------------------- goToLevelChooser = function ( self, event ) local options = { effect = "fromBottom", time = 750, } composer.gotoScene( sceneName, options ) return true end --------------------------------------------------------------------------------- -- Scene Dispatch Events, Etc. --------------------------------------------------------------------------------- function scene:show( event ) local sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willEnter( event ) elseif( willDid == "did" ) then self:didEnter( event ) end end function scene:hide( event ) local sceneGroup = self.view local willDid = event.phase if( willDid == "will" ) then self:willExit( event ) elseif( willDid == "did" ) then self:didExit( event ) end end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) --------------------------------------------------------------------------------- return scene

Post was way too hard to read and seemed to include too many questions.  

Post with the idea that readers will be TL;DR types and you’ll get better answers.

Guessing Primary Question…

My guess is this post essentially boils down to, “My scene doesn’t re-draw when I re-enter as I expect it to.”

How I Make - Redraw on each entry scenes

If I am going to re-draw a scene on each entry, I:

  • do not use create() or destroy()
  • create all content in “will” phase of show() listener
  • destroy all content in “did” phase of hide() listener

Alternate

Others will say (I like my way, but this is cool too), that using create() is the way to go.  If you want to do that, then:

Your Code

Is ‘images’ a global?

If you don’t need to touch those objects outside the scene, then ‘images’ should be a local defined at the top of the file.

Simply set it to {} in create (first)

And clear it in destroy by assigning nil.

Did I Misunderstand?

If I’m way off base, please post below with a super short summary of the problem. A great format to follow is (literally as a bullet list):

  • what I did
  • what I saw
  • what I expected to see
  • why I think it is wrong

PS - Don’t forget, composer.* has a wonderful guide that really gives all the details on how it works, when listeners are called, and in general how best to use it:

https://docs.coronalabs.com/guide/system/composer/index.html

Yes this was asked pretty stupid could have asked much simpler and no you nailed it that is exactly what I was asking.  But what im trying that you suggested is giving me the same results.  I tried two main things:

  1. create all content in “will” phase of show() listener and then destroy all content in “did” phase of hide() listener.  Just copy and pasted all the content that was in create into will phase and added the code below.

  2. kept everything in scene:create then made images local (100% right that it did not need to be global, almost all the variables could be local now that I look at it) then made images=nil in destroy like below.

function scene:destroy( event )

  local sceneGroup = self.view

  images=nil

end

Also of course thanks for all the help.  I was just looking at the composer link you gave.  I read this thing at least 15 times 6 months ago when i started coding and just could not get it at all.  Now im looking at it like the holy grail ive been looking for!!  Thanks a ton.

Don’t overthink this.
 
This is the easiest way to create and later destroy all content:

local content -- variable to hold all content (child groups) function scene:show( event ) if( event.phase == "will" ) then content = display.newGroup() self.view:insert( content ) -- local bgGroup = display.newGroup() content:insert( bgGroup ) local difficultyChooserGroup = display.newGroup() content:insert( difficultyChooserGroup ) ... more groups as needed ... skipping definition of createImage -- No need to track handles unless you are using them createImage(bgGroup, "mainBackground.png", dW\*3, dH, cX, cY) createImage(bgGroup, "mainBottom.png", dW\*.75, dW\*.22, cX, dH\*.85) createImage(bgGroup, "candy.png", dW\*.456, dW \*0.19, cX, dH\*0.26) ... skipping rest of creation steps end end function scene:hide( event ) if( event.phase == "did" ) then display.remove( content ) content = nil end end

Prefer Create
If you prefer to use create() and destroy()

local content -- variable to hold all content (child groups) function scene:create( event ) content = display.newGroup() self.view:insert( content ) -- local bgGroup = display.newGroup() content:insert( bgGroup ) local difficultyChooserGroup = display.newGroup() content:insert( difficultyChooserGroup ) ... more groups as needed ... skipping definition of createImage -- No need to track handles unless you are using them createImage(bgGroup, "mainBackground.png", dW\*3, dH, cX, cY) createImage(bgGroup, "mainBottom.png", dW\*.75, dW\*.22, cX, dH\*.85) createImage(bgGroup, "candy.png", dW\*.456, dW \*0.19, cX, dH\*0.26) ... skipping rest of creation steps end function scene:hide( event ) if( event.phase == "did" ) then if( content ) then timer.performWithDelay( 1, function() composer.removeScene( "nameOfSceneHere" ) end ) end end end function scene:destroy( event ) content = nil end

Side note: I think I said this in another post, but all those weird sizes I see have me worried.
 
Ex: dW*.22

I think you are going to be both surprised and very unhappy with the end result when you run this on a few different devices.

That said, maybe (hopefully) I’m wrong.  :unsure:

I say this because that kind of math generally results in a bunch of sub-pixel rounding issues that cause gaps, overlaps, and/or alignment issues.