Change Scene Not Working Properly

Hi Everyone,

Sorry if this is a silly and obvious question to ask but I have been trying to fix it for over an hour now with no luck.

Anyways my problem is that I am using the director class to switch scenes but every time I do it brings all the images and objects from the previous scene into the new scene except for the button I am pressing! So I was wondering what it is I am doing wrong and how I can fix it.

Here is the code for the my first scene with a button:

module(..., package.seeall)  
  
local localGroup = display.newGroup()  
  
local function newGameBeganFunction()  
end  
local function newGameReleasedFunction()  
 director:changeScene("screen2","crossfade")  
end  
  
local function initVars ()  
  
 local bg = display.newImage ("appBgBlank.png",0, 300)   
  
 local dirt = display.newImage ("floor.png",0,430)  
  
 newGameBTN = movieclip.newAnim({"re-we.png", "re-we.png"})   
 newGameBTN.x = display.contentWidth / 2   
 newGameBTN.y = 5   
 newGameBTN.sound = clickSND   
 newGameBTN.beganFunction = newGameBeganFunction   
 newGameBTN.releasedFunction = newGameReleasedFunction   
 newGameBTN.removeListenerOnTouch = true  
  
 localGroup:insert(newGameBTN)  
  
 newGameBTN:addEventListener("touch", uiButton.handleEvent)   
  
end  
  
function clean ( event )  
 print("cleaned")  
end  
  
function new()  
 initVars()  
 return localGroup  
end  
  

And here is the code for the scene I am going to:

module(..., package.seeall)  
  
local localGroup = display.newGroup()  
  
local function initVars ()  
  
 local dirt = display.newImage ("floor.png",0,430)  
 physics.addBody (dirt, "static",{bounce=0.3})  
  
 local bg = display.newImage ("appBgBlank.png")  
  
 local hero = display.newImage ("re-we.png")  
 hero.x = display.stageWidth/2  
 physics.addBody(hero, {density=3.0, friction=0.5, bounce=0.3 } )  
  
end  
  
function clean ( event )  
 print("cleaned")  
end  
  
function new()  
 initVars()  
 return localGroup  
end  
  

Thanks,

Kevin [import]uid: 2752 topic_id: 7224 reply_id: 307224[/import]

I think you’ll find that you need to insert all of your images (not just the button) in the localGroup so that Director can free them up on changeScene i.e.

localGroup:insert(bg) localGroup:insert(dirt) [import]uid: 8366 topic_id: 7224 reply_id: 25408[/import]

Hi evs,

Thank you so much that worked like a charm! I have one last question however, when I am making objects and displaying images where should I do this? Is it fine to do it within the initvar function or should I do it elsewhere?

Thanks,

Kevin [import]uid: 2752 topic_id: 7224 reply_id: 25413[/import]

Personally I use would use initVars just to set up the level. Then continue with game logic, additional function calls etc within new() [import]uid: 8366 topic_id: 7224 reply_id: 25415[/import]

Alright then I guess thats what I’m gonna start doing, thanks for all the help I really appreciate it!

Kevin [import]uid: 2752 topic_id: 7224 reply_id: 25416[/import]