Removing objects in a function

I have been trying to remove an object that is in a function when I go to the main menu. The falling objects do not disappear when the scene changes. I know that you need to have objects in a group but I don’t know how to insert a object into the group because it is already in a function. I would like to know how to remove the physics objects that are in a function that drops them. Please let me know if you can help. Thanks!


– level1.lua


system.activate(“multitouch”)

local storyboard = require( “storyboard” )

local scene = storyboard.newScene()

local widget = require( “widget” )

– include Corona’s “physics” library

local physics = require “physics”

physics.start(); physics.pause()

physics.setGravity( 0, 6 )


– forward declarations and other locals

local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth*0.5

MainMenuRelease = function( event )

storyboard.gotoScene( “menu”, “fade”, 500 )

return true – indicates successful touch

end

function scene:createScene( event )

local group = self.view

– create a grey rectangle as the backdrop

local background = display.newRect( 0, 0, screenW, screenH )

background.anchorX = 0

background.anchorY = 0

MainMenu = widget.newButton

{

defaultFile = “button.png”,

overFile = “button-over.png”,

label = “Main Menu”,

emboss = true,

onPress = MainMenuPress,

onRelease = MainMenuRelease,

}

MainMenu.x = 100; MainMenu.y = 500

group:insert( background )

group:insert( MainMenu )

end

local function throwBrick()

bricks = display.newImage( “smallcrate.png”, 100 + math.random(200), -100 )

physics.addBody( bricks, { density=3.0, friction=0.5, bounce=0.05 } )

bricks.rotation = math.random(360)

end

timer.performWithDelay( 750, throwBrick, 90000000000000000000)

– Called immediately after scene has moved onscreen:

function scene:enterScene( event )

local group = self.view

physics.start()

end

– Called when scene is about to move offscreen:

function scene:exitScene( event )

local group = self.view

physics.stop()

end

– If scene’s view is removed, scene:destroyScene() will be called just prior to:

function scene:destroyScene( event )

local group = self.view

package.loaded[physics] = nil

physics = nil

if MainMenu then

MainMenu:removeSelf() – widgets must be manually removed

MainMenu = nil

end

end


– END OF YOUR IMPLEMENTATION


– “createScene” event is dispatched if scene’s view does not exist

scene:addEventListener( “createScene”, scene )

– “enterScene” event is dispatched whenever scene transition has finished

scene:addEventListener( “enterScene”, scene )

– “exitScene” event is dispatched whenever before next scene’s transition begins

scene:addEventListener( “exitScene”, scene )

– “destroyScene” event is dispatched before view is unloaded, which can be

– automatically unloaded in low memory situations, or explicitly via a call to

– storyboard.purgeScene() or storyboard.removeScene().

scene:addEventListener( “destroyScene”, scene )


return scene

Are you adding your bricks into your scene’s view group?
 

I have tried but I’m not sure how to add the brick outside of the function. If I add the brick and function it doesn’t work

It looks to me that your throwBricks function is inside the createScene function which should give it access to the “group” group.  You can always access it directly by doing:

scene.view:insert(bricks)

See if that helps you.

That worked perfectly! Thank you very much! One more question! How do you reset a level? Thank you for everything!

Probably the easiest way is to call storyboard.removeScene(“yourscene”) just before you call storyboard.gotoScene(“yourscene”).

This tutorial goes into the details.

http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

Rob

Thank you for everything!

Are you adding your bricks into your scene’s view group?
 

I have tried but I’m not sure how to add the brick outside of the function. If I add the brick and function it doesn’t work

It looks to me that your throwBricks function is inside the createScene function which should give it access to the “group” group.  You can always access it directly by doing:

scene.view:insert(bricks)

See if that helps you.

That worked perfectly! Thank you very much! One more question! How do you reset a level? Thank you for everything!

Probably the easiest way is to call storyboard.removeScene(“yourscene”) just before you call storyboard.gotoScene(“yourscene”).

This tutorial goes into the details.

http://coronalabs.com/blog/2013/08/20/tutorial-reloading-storyboard-scenes/

Rob

Thank you for everything!