Composer not cleaning scene

I am creating a gaming app and when the user goes from the game to the play the next level scene , nothing gets removed . The game scene works ok at times but my real issue is with the play next scene . The walls that I created in game move to the next scene and the background image in the play next scene shows in a little corner . I was using storyboard and this wasn’t happening but when I moved into composer I was getting a lot of issues . Can someone help me to completely remove the scene ? I’ve tried code from my previous composer apps and nothing is helping . I tried showing the error but nothing showed up . This is what I have as of now :

game.lua :

-- requires local physics = require "physics" physics.start() local composer = require( "composer" ) local scene = composer.newScene() function scene:create(event) local screenGroup = self.view local randomImage = math.random(1,41) local background = display.newImageRect("images/background"..randomImage..".jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) display.setDefault("fillColor", 0, 1, 1) CreateWalls(1) -- create image of a ball Ball = display.newCircle(100, 100, 10) physics.addBody(Ball, "dynamic", {friction=2}) Ball:applyLinearImpulse(-.05, .05, 0, 0) screenGroup:insert(Ball) end function scene:show(event) composer.removeScene( "start" ) Ball:addEventListener( "tap", onBallTap ) end function scene:hide(event) end function scene:destroy(event) end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destroy", scene) return scene

next level scene :

-- requires local composer = require( "composer" ) local scene = composer.newScene() -- background function scene:create(event) local screenGroup = self.view local randomImage = math.random(1,41) local background = display.newImageRect("images/background"..randomImage..".jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) local restart = display.newText( "Touch Screen to go to next level", display.contentCenterX, display.contentCenterY, native.systemFont, 25 ) screenGroup:insert(restart) end local options = { effect = "flip", time = 400 } local function touchScreen( event ) if event.phase == "began" then composer.removeScene( "restart" ) composer.gotoScene( "game2", options ) end end function scene:show(event) local phase = event.phase if ( phase == "will" ) then print("Phase started") elseif ( phase == "did" ) then print("phase showing objects") composer.removeScene( "game" ) end Runtime:addEventListener("touch", touchScreen) end scene:addEventListener( "show" ) function scene:hide(event) local sceneGroup = self.view local phase = event.phase Runtime:removeEventListener("touch", touchScreen) end function scene:destroy(event) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

config.lua :

application = { content = { width = 320, height = 480, scale = "zoomStretch", }, }
local background = display.newImageRect("images/background".. screenGroup:insert(background)

Can be reduced to:

local background = display.newImageRect(screenGroup, "images/background"..

CreateWalls(1)

As you haven’t provided the code for this I don’t know what it’s doing, but I bet it’s not adding walls to the screenGroup. Pass it the display group (your screenGroup) to it so it knows  where to put the walls - just like the background:

CreateWalls(screenGroup,1)

You will notice that things you’ve put in the screenGroup will be removed - assuming you’re not holding onto their reference with a global variable.

Any time things are not removed from the screen when using composer it is because they are not actually part of the scene - because they were not added to the scene view (its display group).

I did this :

function scene:create(event) local screenGroup = self.view local randomImage = math.random(1,41) local background = display.newImageRect(screenGroup, "images/background"..randomImage..".jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY display.setDefault("fillColor", 0, 1, 1) CreateWalls(screenGroup,1) -- create image of a ball Ball = display.newCircle(100, 100, 10) physics.addBody(Ball, "dynamic", {friction=2}) Ball:applyLinearImpulse(-.05, .05, 0, 0) screenGroup:insert(Ball) end

and got this error :

bad argument #3 to 'newRect' (number expected, got table) stack traceback: in function 'newRect' C:\Users\user\Documents\Corona Projects\app\game.lua:51: in function 'CreateWalls' C:\Users\user\Documents\Corona Projects\app\game.lua:21: in function '?' This is the function with line 51 where the error is :

function CreateWalls(BorderWidth) -- make the math easier local OldAnchor = {x = display.getDefault(anchorX), y = display.getDefault(anchorY) } SetDefaultAnchor({x=0, y=0}) local Height = display.contentHeight -- + (2 \* BorderWidth) local Width = display.contentWidth -- + (2 \* BorderWidth) local leftWall = display.newRect(0, 0, BorderWidth, Height) -- this is where the error is local rightWall = display.newRect(Width - BorderWidth, 0, BorderWidth, Height) local ceiling = display.newRect(0, 0, Width, BorderWidth) local floor = display.newRect(0, Height-BorderWidth, Width, BorderWidth) physics.addBody (leftWall, "static", {bounce = 0.7, friction = 2}) physics.addBody (rightWall, "static", {bounce = 0.0, friction = 2}) physics.addBody (ceiling, "static", {bounce = 0.8, friction = 2}) physics.addBody (floor, "static", {bounce = 0.0, friction = 2}) -- restore previous defaults SetDefaultAnchor(OldAnchor) end

At the start of CreateWalls() add

print(BorderWidth)

You’ll see that it’s not a number. It needs to be a number.

where do I put the number ? 

I did this :

 local leftWall = display.newRect(50, 50, BorderWidth, Height)

 local Height = 100 -- + (2 \* BorderWidth) local Width = 100 -- + (2 \* BorderWidth)

You have to declare BorderWidth integer value somewhere, put local BorderWidth = (whatever number) at the top of the CreateWalls function. 

Wow, we almost have the same amount of posts.

Also, it looks like the next level scene is a restart feature. Have you considered an overlay?

https://docs.coronalabs.com/api/library/composer/showOverlay.html

https://docs.coronalabs.com/api/library/composer/hideOverlay.html

On an unrelated topic, did your username used to be comeUp264?

Yes it did 

Ok the problems in gam are over , but when I got to the next level scene the picture shows in a small corner of the screen .

This seems to be the issue:

function CreateWalls(BorderWidth)

then you call it:

CreateWalls(screenGroup,1)

CreateWalls is expecting a singular parameter, a number that you use to create a display.newRect(). Since screenGroup (a table) is the first parameter, that ends up populating the variable BorderWidth. You need to either not pass screenGroup in or adjust the function CreateWalls to accept two parameters. Order matters.

Rob

How do I “adjust the function CreateWalls to accept two parameters” ?

Change the function definition from:

function CreateWalls(BorderWidth)

to

function CreateWalls( SceneGroup, BorderWidth )

then you need to update your statements inside the function to use the new parameter you’re passing.

function CreateWalls( ScreenGroup, BorderWidth ) -- make the math easier local OldAnchor = {x = display.getDefault(anchorX), y = display.getDefault(anchorY) } SetDefaultAnchor({x=0, y=0}) local Height = display.contentHeight -- + (2 \* BorderWidth) local Width = display.contentWidth -- + (2 \* BorderWidth) local leftWall = display.newRect( ScreenGroup0, 0, BorderWidth, Height) -- this is where the error is local rightWall = display.newRect( ScreenGroupWidth - BorderWidth, 0, BorderWidth, Height) local ceiling = display.newRect( ScreenGroup0, 0, Width, BorderWidth) local floor = display.newRect( ScreenGroup0, Height-BorderWidth, Width, BorderWidth) physics.addBody (leftWall, "static", {bounce = 0.7, friction = 2}) physics.addBody (rightWall, "static", {bounce = 0.0, friction = 2}) physics.addBody (ceiling, "static", {bounce = 0.8, friction = 2}) physics.addBody (floor, "static", {bounce = 0.0, friction = 2}) -- restore previous defaults SetDefaultAnchor(OldAnchor) end

Rob

I am getting this error :

bad argument #1 to 'newRect' (number expected, got nil) stack traceback: [C]: in function 'newRect' C:\Users\user\Documents\Corona Projects\app\game.lua:50: in function 'CreateWalls'

This is what I have :

 CreateWalls(screenGroup,1) -- I also tried to remove that but the ball didn't show up

function CreateWalls( ScreenGroup, BorderWidth ) -- make the math easier local OldAnchor = {x = display.getDefault(anchorX), y = display.getDefault(anchorY) } SetDefaultAnchor({x=0, y=0}) local Height = display.contentHeight -- + (2 \* BorderWidth) local Width = display.contentWidth -- + (2 \* BorderWidth) local leftWall = display.newRect( ScreenGroup0, 0, BorderWidth, Height) -- this is where the error is local rightWall = display.newRect( ScreenGroupWidth - BorderWidth, 0, BorderWidth, Height) local ceiling = display.newRect( ScreenGroup0, 0, Width, BorderWidth) local floor = display.newRect( ScreenGroup0, Height-BorderWidth, Width, BorderWidth) physics.addBody (leftWall, "static", {bounce = 0.7, friction = 2}) physics.addBody (rightWall, "static", {bounce = 0.0, friction = 2}) physics.addBody (ceiling, "static", {bounce = 0.8, friction = 2}) physics.addBody (floor, "static", {bounce = 0.0, friction = 2}) -- restore previous defaults SetDefaultAnchor(OldAnchor) end

Not that I intentionally added bugs to your code, but I made a simple typo so let’s take this as a learning opportunity.

Look at the error:

bad argument #1 to 'newRect' (number expected, got nil) stack traceback: [C]: in function 'newRect' C:\Users\user\Documents\Corona Projects\app\game.lua:50: in function 'CreateWalls'

Bad argument #1 to ‘newRect’ (number expected, got nil)

This is telling you that the first parameter to some call to display.newRect() is nil when a number was expected. This function can take a table as it’s first parameter as long as it’s a display.newGroup() object. So why is a nil getting passed? And where is the issue?

Looking further down, it says:

game.lua:50: in function ‘CreateWalls’

Open game.lua in your editor and find line 50. This is much easier if you’re using an editor that supports line numbers and those line numbers are turned on.  I suspect it’s this line:

 local leftWall = display.newRect( ScreenGroup0, 0, BorderWidth, Height) -- this is where the error is 

Looking at that line, what is the first parameter passed to display.newRect()?  It’s a variable named ScreenGroup0. Do you have a variable with that name? No, you don’t. You have a variable ScreenGroup being passed to your function. The original code wasn’t attempting to add these objects to a group and used to read:

 local leftWall = display.newRect( 0, 0, BorderWidth, Height) -- this is where the error is 

Comparing the two, it would appear that I missed a comma when I attempted to add “ScreenGroup” and a new first parameter.

Now the corrected line should be:

 local leftWall = display.newRect( ScreenGroup, 0, 0, BorderWidth, Height) -- this is where the error is 

See how it’s different now? ScreenGroup which is the name of a real variable/parameter in your function is properly separated from the first number by a comma.

If you fix this error, you will still have more of them. While I tried to be careful to copy the comma and trailing space, it looks like I missed then when I made my copy and I pasted the error in multiple times.

Rob

Thanks it’s good now . My other problem is with my restart screen . When I go from game to restart , the background image shows in a small square on the bottom right . This is what I have :

-- requires local composer = require( "composer" ) local scene = composer.newScene() -- background function scene:create(event) local screenGroup = self.view local randomImage = math.random(1,41) local background = display.newImageRect("images/background"..randomImage..".jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY screenGroup:insert(background) local restart = display.newText( "Touch Screen to go to next level", display.contentCenterX, display.contentCenterY, native.systemFont, 25 ) screenGroup:insert(restart) end local options = { effect = "flip", time = 400 } local function touchScreen( event ) if event.phase == "began" then composer.removeScene( "restart" ) composer.gotoScene( "game2", options ) end end function scene:show(event) local phase = event.phase if ( phase == "will" ) then print("Phase started") elseif ( phase == "did" ) then print("phase showing objects") composer.removeScene( "game" ) end Runtime:addEventListener("touch", touchScreen) end scene:addEventListener( "show" ) function scene:hide(event) local sceneGroup = self.view local phase = event.phase Runtime:removeEventListener("touch", touchScreen) end function scene:destroy(event) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Is the square something you’re creating in another scene? If so you may not be adding it to your scene group (screenGroup).

Rob

No I want my background to show on the whole screen but when I go into any other scene but game.lua it shows on the bottom right of the screen in a rectangle (not square sorry). It is added to scene group

Clearly it’s not doing what you want. When debugging programs and in particular when asking for help from others, we have to separate what you want to happen and what is happening. What is happening is you’re getting a unexpected rectangle. That can only happen from either a display.newRect() call that’s creating the rectangle or possibly a display.newImageRect().

What you are describing is that perhaps you’re creating a rectangle in the scene you are leaving and you’re not inserting it into the scene’s “view” group (screenGroup). These objects sit on top of everything else. Look in the scene you are leaving and make sure any display.* object  you create gets inserted into the group correctly.

Rob

Everything is inserted correctly . Look :

function scene:create(event) local screenGroup = self.view local randomImage = math.random(1,41) local background = display.newImageRect(screenGroup, "images/background"..randomImage..".jpg",display.contentWidth,display.contentHeight) background.x = display.contentCenterX background.y = display.contentCenterY CreateWalls(screenGroup,1) display.setDefault("fillColor", 0, 1, 1) -- create image of a ball Ball = display.newCircle(100, 100, 10) physics.addBody(Ball, "dynamic", {friction=2}) Ball:applyLinearImpulse(-.05, .05, 0, 0) screenGroup:insert(Ball) end

function scene:show(event) composer.removeScene( "start" ) Ball:addEventListener( "tap", onBallTap ) end function scene:hide(event) end function scene:destroy(event) end scene:addEventListener("create", scene) scene:addEventListener("show", scene) scene:addEventListener("hide", scene) scene:addEventListener("destroy", scene) return scene
local background = display.newImageRect("images/background".. screenGroup:insert(background)

Can be reduced to:

local background = display.newImageRect(screenGroup, "images/background"..

CreateWalls(1)

As you haven’t provided the code for this I don’t know what it’s doing, but I bet it’s not adding walls to the screenGroup. Pass it the display group (your screenGroup) to it so it knows  where to put the walls - just like the background:

CreateWalls(screenGroup,1)

You will notice that things you’ve put in the screenGroup will be removed - assuming you’re not holding onto their reference with a global variable.

Any time things are not removed from the screen when using composer it is because they are not actually part of the scene - because they were not added to the scene view (its display group).