0
votes

I created a local group and insert objects in it on the screen as a rectangle and then use myGroup:removeSelf() and myGroup = nil. Automatically memory for the rectangle and all other elements will be emptied too? (next code)

cenarioGrupo = display.newGroup()

local chao = display.newRect( display.contentWidth*0.5, display.contentHeight*0.95, display.contentWidth, display.contentHeight*0.1 )[

cenarioGrupo:insert(chao)

--Then..
cenarioGrupo:removeSelf();   cenarioGrupo = nil;

and other question. How can I use the cenarioGrupo in createScene function, and it is only created in function criarCenario? Returning it? Creating it overall?

local function criarCenario()
    cenarioGrupo = display.newGroup()

    local chao = display.newRect( display.contentWidth*0.5, display.contentHeight*0.95, display.contentWidth, display.contentHeight*0.1 )
    chao:setFillColor(1,1,1)

    cenarioGrupo:insert(chao)
end


function scene:createScene( event )
      local sceneGroup = self.view
      criarCenario()
end
1

1 Answers

0
votes

in Corona if you create a display group and add display objects (not native widget of android) to it, when you try to remove display group all of it's children and containments will be erased, too.

for your second question: you can use sceneGroup as an entry for your criarCenario, like this:

function scene:createScene( event )
    local sceneGroup = self.view
    criarCenario(sceneGroup)
end

and then in your function just insert your display group to sceneGroup:

local function criarCenario(sceneGroup) -- use an entry
    cenarioGrupo = display.newGroup()


local chao = display.newRect( display.contentWidth*0.5, display.contentHeight*0.95, display.contentWidth, display.contentHeight*0.1 )


 chao:setFillColor(1,1,1)
cenarioGrupo:insert(chao)
sceneGroup:insert(cenarioGrupo) -- here is the main change
end

you can also do that by returning your cenarioGrupo and insert it to sceneGroup in createScene:

local function criarCenario()
    cenarioGrupo = display.newGroup()
    local chao = display.newRect( display.contentWidth*0.5, display.contentHeight*0.95, display.contentWidth, display.contentHeight*0.1 )
    chao:setFillColor(1,1,1)

    cenarioGrupo:insert(chao)
    return cenarioGrupo
end

function scene:createScene( event )
      local sceneGroup = self.view
      sceneGroup:insert( criarCenario() )
end

I myself prefer the second method because it provides you more loose coupling. your criarCenario function is more separated from createScene in the second method.