Lets say you’re trying to be nice and tidy about it and you want to have a number of display groups controlled to show only one at a time. What should you do?
The answer is obvious - you put them all in the same parent group and have a function to switch between them. The problem is that you might end up with a huge list of display group creating statements. Ok, not a massive problem, but here is a way of keeping it tidy:
The groups with a coloured circle in each, cycled by calling a show/hide function.
main.lua:
[lua]require(“groups”)
local stage = display.getCurrentStage()
local groupA, groupB, groupC = unpack( createDisplayGroups( stage, 3 ) )
display.newCircle(groupA,100,100,50):setFillColor( 255,0,0 )
display.newCircle(groupB,100,100,50):setFillColor( 0,255,0 )
display.newCircle(groupC,100,100,50):setFillColor( 0,0,255 )
showGroup( groupA )
timer.performWithDelay( 3000, function() showGroup( groupB ) end, 1 )
timer.performWithDelay( 6000, function() showGroup( groupC ) end, 1 )[/lua]
groups.lua:
[lua]-- display group management
–[[
Assign your display groups to variables like this:
local groupA, groupB, groupC = unpack( createDisplayGroups( display.getCurrentStage(), 3 ) )
]]–
– creates a series of display groups
function createDisplayGroups( parent, count )
local list = {}
for i=1, count do
local group = display.newGroup()
list[#list+1] = group
parent:insert( group )
end
return list
end
– hides the other groups in the parent and shows this one
– child display groups can have a function called ‘showHide( isHidden )’ which will be called when the group is shown or hidden
– if the showHide function returns true nothing more is done, if nil or false is returned the showGroup function will set the child.isVisible property appropriately
function showGroup( group )
local parent = group.parent
for i=1, parent.numChildren do
local child = parent[i]
if (child.showHide == nil or child:showHide( child == group ) ~= true) then
child.isVisible = (child == group)
end
end
end[/lua]
I guess this is a really simple version of storyboarding, but I find it useful in situations where going full tilt at the problem only adds unnecessary complexity. This is small and easy to read. It also works with widgets. [import]uid: 8271 topic_id: 27260 reply_id: 327260[/import]