Create multiple groups, group those groups into master group and remove master group?

Ok so yea, the title says it all

I created a group called ui and game. I then created a master group called Mgroup.

I then made a start button that removes the ui group, which in this case is the start button and the instruction text about the start button.

I then made a remove button that shows text at the bottom, that removes EVERYTHING that’s part of the master group (Mgroup)

phew, that was confusing. It works, but I don’t know if it’s right, or what but I was having some fun so yea here you go.

View on ipad in simulator to see everything :slight_smile:

Don’t give me a hard time about indenting, and formatting and clean code this isn’t a clean code type of thing, just something I have noticed and want to know is it a valid way of doing things?

ng :slight_smile:

EDIT ** 10/07/11 - I added these 2 things:

[lua] ui = nil
Mgroup = nil[/lua]

So if I removeSelf() and I nil out the groups, that would cover it I would think? For what it’s worth with or without the nil on the groups, the memory still goes down to 79.904 in either case when I remove the Mgroup.

Also, I feel like I am doing what director is doing or something but doing it the long way (or my way) lol. I’ve been hesitant to use any 3rd party tools as I want to learn everything natively in Corona SDK. I don’t know if that’s making me lose time or what, or if I should just use director? Hmmmmm

Here is the updated code with the nil on the ui and Mgroup
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0, 0)

–Make the physics collisions more accurate (default is 8, higher is lower performance
–but better collision detection
physics.setPositionIterations(32)

–shortcut variables
local _W = display.contentWidth
local _H = display.contentHeight

–The game group is for all of the game elements
–player, walls of the level and all things contained within that’s not an UI element
local game = display.newGroup()
game.x = 0
game.y = 0

–ui group - this is for all of the options like start, reset, etc
–this will be called to removeSelf() on these elements
local ui = display.newGroup()
ui.x = 0
ui.y = 0

–All groups
local Mgroup = display.newGroup (game, ui)

–put the ball in game
local ball1 = display.newCircle (100, 50, 40)
ball1:setFillColor (0,0,255)
ball1.x = _W/2 - 5
ball1.y =90
ball1.alpha = 1
game:insert(ball1)
physics.addBody(ball1, “static”)

–greenbox
local greenbox = display.newRect( _W/2, _H/2 +200, 50,50)
greenbox:setFillColor (0,255,0)
game:insert(greenbox)
physics.addBody(greenbox, “static”)

–red box
local redbox = display.newRect( _W/2 +100, _H/2, 50,50)
redbox:setFillColor (255,0,0)
game:insert(redbox)
physics.addBody(redbox, “static”)

–bluebox
local bluebox = display.newRect( _W/2 +150, _H/2 + 150, 50,50)
bluebox:setFillColor (0,0,255)
game:insert(bluebox)
physics.addBody(bluebox, “static”)

–Startbutton
local startButton = display.newRect ( _W/2 -40 , _H/2, 80, 80)
local startText = display.newText( “START”, startButton.x -35, startButton.y-20, nil, 22 )
startText:setTextColor(0,0,255)
ui:insert(startButton)
ui:insert(startText)
physics.addBody(startButton ,“static”)

local removeButton = display.newRect ( _W/2 -300 , _H/2, 180, 80)
local removeText = display.newText( “REMOVE”, removeButton.x -35, removeButton.y-20, nil, 22 )
removeText:setTextColor(255,0,255)
Mgroup:insert(removeButton)
Mgroup:insert(removeText)
physics.addBody(removeButton ,“static”)

–text that explains stuff at the top
local infoText = display.newText( “Press Start - removes the ui group”, _W/2- 300, ball1.y+50, nil,40 )
local infoText2 = display.newText( “(removes start button and instruction text only)”, _W/2- 350, ball1.y+100, nil,30 )
ui:insert(infoText)
ui:insert(infoText2)

–Text that shows on the bottom of screen
local infoText3 = display.newText( “Press Start - removes the ui and game group”, _W/2- 350, startButton.y + 300, nil,30 )
local infoText4 = display.newText( “(will remove the remove button, start, red/blue/green and ball1)”, _W/2- 300, startButton.y + 350, nil,20)
Mgroup:insert(infoText3)
Mgroup:insert(infoText4)

–Function that removes the UI group like start button etc
–Also changes gravity for dynamic objects to 9.8, which in this case is the ball and it falls…yay!

–Start button, pressing it removes the ui group for startbutton,and text
local function pressStart(event)
if event.phase == “ended” then
physics.setGravity( 0,9.8 )
ball1.bodyType = “dynamic”

ui:removeSelf()
ui = nil

end
end

–Removes all groups (ui, game)
local function removeGroups(event)
if event.phase == “ended” then

Mgroup:removeSelf()
Mgroup = nil

end
end

–touch event to initiate gravity change because its fun
–Also calls the function to remove the ui group (start button and text)
startButton:addEventListener( “touch”, pressStart )

–Remove ALL GROUPS
removeButton:addEventListener( “touch”, removeGroups )

—=========================================================
—=========================================================
—=============GARBAGE COLLECTION STUFF===================

local monitorMem = function()
collectgarbage()

print( "MemUsage: " … collectgarbage(“count”) )

local textMem = system.getInfo( “textureMemoryUsed” ) / 1000000

print( "TexMem: " … textMem )

end

Runtime:addEventListener( “enterFrame”, monitorMem )

—=========================================================
—=========================================================
—==========END GARBAGE COLLECTION STUFF===================[/lua] [import]uid: 61600 topic_id: 16047 reply_id: 316047[/import]

Nowhere are you setting these display objects to nil. So even though they are getting removed from the stage, the memory allocated to these objects is still being held. The result will be a memory leak.

Check this out: http://blog.anscamobile.com/2011/08/corona-sdk-memory-leak-prevention-101/ [import]uid: 70391 topic_id: 16047 reply_id: 59640[/import]

@nicholas,

  1. if you are after the touch ended phase only, then use a tap instead of touch

  2. IMO, the removeSelf() should clean up everything for you in the newer versions and you might have to call the garbageCollection to free the memory.

  3. yes, that should work (IMO)

cheers,

?:slight_smile: [import]uid: 3826 topic_id: 16047 reply_id: 59665[/import]