Creating display objects in a separate module and then using that module to create and display those objects in different Composer scenes (“independently”) just seems like a really basic task. I’ve googled this but apart from sharing primitives (numbers, strings) and booleans etc, I can’t find any relevant info on how to do the same with display objects. Am I approaching this whole menu-thing completely wrong? What would be the textbook way of doing this?
very generally, cuz i’m lazy, one possible pattern goes like this:
-- Menu.lua local Menu = {} local Menu\_mt = { \_\_index = Menu } -- note that this is just a "dot'ed" function -- we don't strictly need "self" (which would refer to the class Menu, which we already have access to in this scope anyway) -- (this is akin to a "static method" in other language's jargon) function Menu.new(parentGroup, whatever\_other\_params\_you\_need) local instance = setmetatable({}, Menu\_mt) -- instance now picks up all of Menu's methods instance.group = display.newGroup() parentGroup:insert(instance.group) return instance end -- note that this is a "colon'ed" function -- we DO need "self" (which refers to the particular instance) function Menu:destroy() display.remove(self.group) self.group = nil end -- note that this is a "colon'ed" function -- we DO need "self" (which refers to the particular instance) function Menu:setVisible(value) self.group.isVisible = value end return Menu
and
-- scene.lua -- (lots of non-relevant details left out) local Menu = require("Menu") -- this is the class local menu = nil -- this WILL BE the instance, for THIS scene alone -- in create menu = Menu.new(scene.view) -- in show menu:setVisible(true) -- in hide menu:setVisible(false) -- in destroy menu:destroy() menu = nil
fwiw, at this point the whole notion of menu:setVisible() become superflous anyway if you’re only doing it in response to scene.show/hide
you’re showing/hiding the entire scene group, so no need to explicitly/separately show/hide the menu within it
Hi,
I have not implemented this yet, but my plan is to create a “menu” module in main.lua and attach it to the Composer object using composer.setVariable, and retrieve it when needed using composer.getVariable.
If you go this route, and it works, let me know. 
-dev
I’m late to the conversation. If you want the menu always visible, simply don’t put it in a scene view group. Display objects that are not in a composer group are drawn at the highest level of OpenGL. native.* will be on top of that. We call this HUD mode. It’s used frequently by apps implementing a tabBar menu at the bottom of the page.
You can have a method in your menu module that you could have scenes call when they show that changes the content of the menu. This way the menu is always on the screen and isn’t effected by scene transitions
I think @develepant is heading in that direction in his post above.
-- menu.lua local M = {} function M.new() -- create your display objects function M.update() -- change the values of the display objects based on the scene -- you will need to figure out the parameters needed to pass here.
In main.lua:
local menu = require("menu") local myMenu = menu.new(params)
in random scene:
local menu = require("menu") ... function scene:show( event ) if event.phase == "did" then menu.update(params) end end
or something like that.
Of course I could be completely off base on what you’re trying to do.
Rob
First of all, thanks for all your input!
I’ decided to go on davebollinger’s template since I wanted to learn anyway how to create and instantiate independent “classes” in lua (given my previous experiences with the Java language).
@Develephant: that was actually my next idea if I couldn’t get the “OOP-approach” working. If I understand you correctly, your approach would be to create the menu once and then just reuse it by passing the menu object (or display group) from scene to scene. That would practically be the same approach as taken by Sphere Game Studios, but without the use of globals, right?
@Rob: no, you are spot on what I wanted to acheive but I have to add the menu to Composer’s scene group since other objects need to be able to move in front of the menu.
Now that I have it all working, I realize that the main thing that messed it up for me was how/when to use “self” and the whole dot/colon annotation, especially when used from within the instantiated class calling its own functions (like a listener). I will read up on this since it is essential to understand, I think.
Again, thanks for all your help!