you dont provide any code, sample or error messages, so its impossible for anyone to try and figure out whats wrong
If I just said my code doesn’t work what’s wrong then it would be, but clearly I haven’t?
Ok, I just wondered if anyone knew whether objects added to the scene group would be removed automatically on scene exit or not. Sometimes code is necessary it’s true, other times someone just says "of course the objects are not removed completely on scene exit’ or whatever.
Composer does not care what display objects are added to scene group, it will destroy them all (mwahahaha).
Something else is wrong, perhaps some code is being run twice which could be for a number of reasons - a button touched twice in quick succession, or not checking for ‘began’ or ‘ended’ phase on a touch listener are two examples.
Ok I see, thanks. That’s what I thought so it’s a bit odd. Does it really destroy them though or just hide them? Because I noticed scene:hide get’s called (by just adding a print in) but scene:destroy doesn’t print anything if I add a print in there so I wasn’t sure if that always gets called or not.
You have a point in that objects will only be destroyed if the scene itself is destroyed (I always do this when leaving a scene, some others don’t), so the next question is where do you create your objects? I believe scene:create is only called when a scene is first instantiated, but scene:show is run every time the scene is displayed.
Make sure the text is added to the scene’s group.
Yes it is definitely added, like I was saying in my first post.
Yes so it’s odd. Even if it was just being hidden on scene exit rather than being destroyed surely it should just update the existing text when running score.text = newscore rather than creating another text object on top.
Where do you create the object score?
Here’s the code for the game scene:
-- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then Game.initialize() end
This the function called:
Game = { initialize = function() sceneGroup = composer.getScene( "scene.game" ).view movesText = display.newText( { text = moves, x = 100, y = 50, width=40, align="left", font="Optima-Bold", fontSize = 14 } ) movesText:setFillColor(0.88,0.88,0.88) sceneGroup:insert(movesText) end,
There’s your answer. Scene:show is called every time the scene is displayed, whether that’s the first time or not. Display objects should be set up in Scene:create.
By default scenes are not purged from memory when moving to another.
Oh right I see. I had it that way originally but I had to make the ‘moves’ widget and the ‘movesText’ variable global or otherwise the moves amount and movesText could not be updated within the game code.
No need to make them global. Forward declare them locally at the top of your file, and then assign the actual objects when ready,
[lua]
local myVariable
[/lua]
Later on…
[lua]
myVariable = display.newRect…
[/lua]
Yes I have them forward declared in the game scene at the top but the Game.initialize and other game functions are all in another big file called game.lua. Thus the forward declared variables aren’t accessible there. The game.lua is required in the main.lua.
Composer hides things by default. If you don’t want that try this:
composer.gotoScene( "scene.Settings", options ) -- Go to Settings screen -- force composer to issue destroy on Intro screen timer.performWithDelay(500, function () composer.removeScene( "scene.Intro" ); end , 1)