首页 文章

CORONA SDK(LUA)中的displayGroups

提问于
浏览
0

我创建了一个本地组,并在屏幕上以矩形形式插入对象,然后使用myGroup:removeSelf()和myGroup = nil . 矩形和所有其他元素的自动记忆也将被清空? (下一个代码)

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;

和其他问题 . 如何在createScene函数中使用cenarioGrupo,它只在函数criarCenario中创建?回来了吗?整体创造?

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 回答

  • 0

    在Corona中如果你创建一个显示组并添加显示对象(不是android的原生小部件),当你试图删除显示组时,所有它的子节点和包含也将被删除 .

    对于你的第二个问题:你可以使用sceneGroup作为criarCenario的一个条目,如下所示:

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

    然后在你的函数中只需将你的显示组插入到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
    

    您也可以通过返回cenarioGrupo并将其插入到createScene中的sceneGroup来实现:

    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
    

    我自己更喜欢第二种方法,因为它为你提供了更松散的耦合 . 你的criarCenario函数在第二种方法中与createScene更加分离 .

相关问题