Objects staying on screen when the scene is changed

Hello, I’m new to Corona so I apologize if this is an obvious question.

I’m having an issue where I have objects within a scene that stay on screen when the scene is changed by a “next button”.

The objects are free to move by tilting the device and are kept in the frame by static bar elements.

I think I need to have a “remove elements” type control on the next button but not sure how to write this.

I have included my code below, and would appreciate nay input if possible. Thanks for the help.

[code]
module(…, package.seeall)
function new()
local localGroup = display.newGroup()
local physics = require(“physics”)
physics.start()

physics.setScale( 60 )
physics.setGravity( 0, 9.8 ) – initial gravity points downwards

system.setAccelerometerInterval( 100 ) – set accelerometer to maximum responsiveness
function onTilt( event )
physics.setGravity( ( -5.8 * event.xGravity ), ( -5.8 * event.yGravity ) )
end

Runtime:addEventListener( “accelerometer”, onTilt )
display.setStatusBar( display.HiddenStatusBar )
local background = display.newImage (“mountain.png”)

borderBodyElement = { friction=0.5, bounce=0.3 }

local borderTop = display.newRect( 0, 0, 1025, 10 )
borderTop:setFillColor( 225, 225, 225, 225) – make invisible
physics.addBody( borderTop, “static”, borderBodyElement )

local borderBottom = display.newRect( 0, 755, 1025, 20 )
borderBottom:setFillColor( 225, 225, 225, 225) – make invisible
physics.addBody( borderBottom, “static”, borderBodyElement )

local borderLeft = display.newRect( 0, 0, 20, 760 )
borderLeft:setFillColor( 225, 225, 225, 225) – make invisible
physics.addBody( borderLeft, “static”, borderBodyElement )

local borderRight = display.newRect( 1005, 0, 20, 760 )
borderRight:setFillColor( 225, 225, 225, 225) – make invisible
physics.addBody( borderRight, “static”, borderBodyElement )

–local triangle = display.newImage(“triangle.png”)
–triangle.x = 80; triangle.y = 160

–local triangle2 = display.newImage(“triangle.png”)
–triangle2.x = 170; triangle2.y = 160

–local pentagon = display.newImage(“pentagon.png”)
–pentagon.x = 80; pentagon.y = 70

–local pentagon2 = display.newImage(“pentagon.png”)
–pentagon2.x = 170; pentagon2.y = 70

local crate = display.newImage(“crate.png”)
crate.x = 150; crate.y = 850

local crateB = display.newImage(“crateB.png”)
crateB.x = 250; crateB.y = 450

local crateC = display.newImage(“crateC.png”)
crateC.x = 260; crateC.y = 10

local soccerball = display.newImage(“soccer_ball.png”)
soccerball.x = 80; soccerball.y = 320

–local superball = display.newImage(“super_ball.png”)
–superball.x = 260; superball.y = 940

–local superball2 = display.newImage(“super_ball.png”)
–superball2.x = 240; superball2.y = 730

–local superball3 = display.newImage(“super_ball.png”)
–superball3.x = 250; superball3.y = 580
–triangleShape = { 0,-35, 37,30, -37,30 }
–pentagonShape = { 0,-37, 37,-10, 23,34, -23,34, -37,-10 }

physics.addBody( crate, { density=2, friction=0.5, bounce=0.4 } )
physics.addBody( crateB, { density=4, friction=0.5, bounce=0.4 } )
physics.addBody( crateC, { density=1, friction=0.5, bounce=0.4 } )

–physics.addBody( triangle, { density=0.9, friction=0.5, bounce=0.3, shape=triangleShape } )
–physics.addBody( triangle2, { density=0.9, friction=0.5, bounce=0.3, shape=triangleShape } )

–physics.addBody( pentagon, { density=0.9, friction=0.5, bounce=0.4, shape=pentagonShape } )
–physics.addBody( pentagon2, { density=0.9, friction=0.5, bounce=0.4, shape=pentagonShape } )

physics.addBody( soccerball, { density=0.9, friction=0.5, bounce=0.6, radius=38 } )
–physics.addBody( superball, { density=0.9, friction=0.5, bounce=0.8, radius=24 } )

–physics.addBody( superball2, { density=0.9, friction=0.5, bounce=0.8, radius=24 } )
–physics.addBody( superball3, { density=0.9, friction=0.5, bounce=0.8, radius=24 } )

local backbutton = display.newImage (“back.png”)
backbutton.x = 100
backbutton.y = 670

local nextbutton = display.newImage (“next.png”)
nextbutton.x = 930
nextbutton.y = 670
local function pressBack (event)
if event.phase == “ended” then
director:changeScene (“farm”)

end
end

local function pressNext (event)
if event.phase == “ended” then
director:changeScene (“town”)

end
end
localGroup:insert(background);

localGroup:insert(backbutton);
localGroup:insert(nextbutton);
backbutton:addEventListener (“touch”, pressBack);
nextbutton:addEventListener (“touch”, pressNext);

return localGroup
end
[/code] [import]uid: 98504 topic_id: 17494 reply_id: 317494[/import]

just insert all your display objects into localGroup and they will be removed automatically upon changing scene [import]uid: 16142 topic_id: 17494 reply_id: 66418[/import]

oh ok… ill try that. thank you for getting back to me. i appreciate the help others give newbies. [import]uid: 98504 topic_id: 17494 reply_id: 66419[/import]

As darkconsoles said put all your displayed objects into a group and when you change scenes the objects inside group will be removed.

Example:
[lua]local crate = display.newImage(“crate.png”)
crate.x = 150; crate.y = 850
localGroup:insert(crate) [import]uid: 30314 topic_id: 17494 reply_id: 66420[/import]

Thank you both! That worked perfectly!! [import]uid: 98504 topic_id: 17494 reply_id: 66422[/import]