I am trying to setup multiple groups for each of the scene of my game:
Group1 = Menu
Group2 = Play Scene etc.
I have some objects that really belong to both Groups such as the background. Unfortunately when I make one visible say the menu and the other one invisible then some of the objects that are supposed to be in the play scene obviously don’t appear. What’s the proper way to do this. Any suggestions?
Sorry for the stupid questions but why isn’t this working?
local startPlayScene = function()
menu.isVisible = true
playScene.isVisible = false
end
local loading = function ()
–Initializing Graphic Groups
local menu = display.newGroup ()
menu.isVisible = true
local playScene = display.newGroup ()
playScene.isVisible = false
local gameOver = display.newGroup ()
gameOver.isVisible = false
local pauseGame = display.newGroup ()
pauseGame.isVisible = false
…
end
When I call the starPlayScene by clicking a button nothing happens. Here is the code for the button which is inside the loading function
local playButton = ui.newButton{
default = “Play button_s01.png”,
over = “Play button_s03.png”,
onRelease = startPlayScene,
}
Local variables you define in a Function is local to that function. You need to define them outside your function for it to work. Here’s an example modified from your code.
-- forward references
local menu, playScene
local startPlayScene = function()
menu.isVisible = false
playScene.isVisible = true
end
local loading = function ()
--Initializing Graphic Groups
menu = display.newGroup ()
menu.isVisible = true
playScene = display.newGroup ()
playScene.isVisible = false
local r = display.newRect( 15, 70, 280, 70 ) -- x,y,w,h
r:setFillColor( 255, 255, 255 )
menu:insert(r)
local a = display.newCircle( 100, 100, 50) -- x,y, radius
a:setFillColor( 255, 255, 255 )
playScene:insert(a)
end
loading()
-- Call Play function after 3 seconds
timer.performWithDelay( 3000, startPlayScene )
You should have seen an error in the terminal when you ran your program.
BTW, a display object can only be in one group at a time. Groups can be children of other groups.