Hi,
I have three Composer scenes in my game and I want to display the same menu buttons in all three. Therefore, I have created a separate module called menu.lua. The calls to menu.lua are the same in all three scenes:
local menu = require("menu.lua") function scene:create(event) ... local menuGroup = menu:create() sceneGroup:insert(menuGroup) ... end function scene:show(event) local phase = event.phase if (phase == "will") menu:setMenuVisible(true) elseif (phase == "did") then ... end ... end function scene:hide(event) local phase = event.phase if (phase == "will") menu:setMenuVisible(false) elseif (phase == "did") then ... end end
The code in menu.lua is:
local M = {} function M:create() -- Set up self.soundButtonDisplayGroup = display.newGroup() self.menuGroup = display.newGroup() -- Set sound volume control button self.soundButton = display.newImageRect("soundbutton.png", 50, 50) self.soundButton.x = 100 self.soundButton.y = 100 self.soundButtonDisplayGroup:insert(self.soundButton) self.soundButton.touch = onSoundButtonTouch self.soundButton:addEventListener("touch", self.soundButton) self.menuGroup:insert(self.soundButtonDisplayGroup) ... return self.menuGroup end function M:setMenuVisible(visible) if (visible) then self.menuGroup.isVisible = true else self.menuGroup.isVisible = false end end function M.onSoundButtonTouch(self, event) -- Functionality for toggling music on and off return true end return M
The problem with this is that the menu disappears when going from one scene to another and then back. If I omit setMenuVisible() then the menu from one scene will be visible in another, below the new one.
What I want to achieve is to have three completely “independent” menu instances of the menu “class” that are displayed and used with no “relations” to the menus in the other scenes. How can I do this?
