I have two different scenes, one mainScene and one inventoryScene. Inside the mainScene I want to load a “buttons” module and that module creates all the buttons needed. But since I called this module from mainScene, i want the buttons to be added to the mainScene as well. But I can’t accomplish that since I can’t pass “sceneGroup” to this module. Is there any way to pass this variable to the buttons module?
This is kind of a deep subject because there are multiple way’s to accomplish it. Your buttons module could create the buttons, put them in a group and then the module could return that group. Maybe something like:
-- makebuttons.lua local M = {} function M.create() local group = display.newGroup() ... code to create buttons and insert them into "group" ... return group end return M
then when you want to get the buttons in scene A
local makeButtons = require("makebuttons") function scene:create( event ) local sceneGroup = self.view local buttons = makeButtons.create() sceneGroup:insert( buttons ) end
You would of course have to work that into your own code.
How about something simple like this in your buttons module?
All you do is pass it a reference to the display group you want the buttons inserted into - be that your own group or the corona scene group.
function createButtons(displayGroup) --create buttons local button1 = widget.newButton({left = 100, top = 200, id = "button1", label = "Button1"}) displayGroup:insert(button1) local button2 = widget.newButton({left = 100, top = 300, id = "button2", label = "Button2"}) displayGroup:insert(button2) end
Personally, and for the sake of simplicity and clarity, I really prefer using the Composer “setVariable()” and “getVariable()” functions. Set it once, get it anywhere else within a Composer-enabled module. Change them if needed, add to them, or even “nil” them out if you don’t need them anymore. So, so, so easy, I doubt I’d ever use another way (assuming I was using Composer). You can even, of course, store tables of multiple values or complex nested tables under one variable name, and then pick them apart in the receiving scene as needed.
Knowing this will make the variable transitions between scenes very easy. But for this problem creating a function to add the scenes seems better and easier to understand in my opinion.
local buttons = require("buttons"); buttons.add\_buttons\_to\_scene(sceneGroup);
is clearer than adding sceneGroup to composer variables and then inside the buttons file using the sceneGroup then removing it. But nevertheless I’ll use getVariable and setVariable. Thanks a lot.
This is kind of a deep subject because there are multiple way’s to accomplish it. Your buttons module could create the buttons, put them in a group and then the module could return that group. Maybe something like:
-- makebuttons.lua local M = {} function M.create() local group = display.newGroup() ... code to create buttons and insert them into "group" ... return group end return M
then when you want to get the buttons in scene A
local makeButtons = require("makebuttons") function scene:create( event ) local sceneGroup = self.view local buttons = makeButtons.create() sceneGroup:insert( buttons ) end
You would of course have to work that into your own code.
How about something simple like this in your buttons module?
All you do is pass it a reference to the display group you want the buttons inserted into - be that your own group or the corona scene group.
function createButtons(displayGroup) --create buttons local button1 = widget.newButton({left = 100, top = 200, id = "button1", label = "Button1"}) displayGroup:insert(button1) local button2 = widget.newButton({left = 100, top = 300, id = "button2", label = "Button2"}) displayGroup:insert(button2) end
Personally, and for the sake of simplicity and clarity, I really prefer using the Composer “setVariable()” and “getVariable()” functions. Set it once, get it anywhere else within a Composer-enabled module. Change them if needed, add to them, or even “nil” them out if you don’t need them anymore. So, so, so easy, I doubt I’d ever use another way (assuming I was using Composer). You can even, of course, store tables of multiple values or complex nested tables under one variable name, and then pick them apart in the receiving scene as needed.
Knowing this will make the variable transitions between scenes very easy. But for this problem creating a function to add the scenes seems better and easier to understand in my opinion.
local buttons = require("buttons"); buttons.add\_buttons\_to\_scene(sceneGroup);
is clearer than adding sceneGroup to composer variables and then inside the buttons file using the sceneGroup then removing it. But nevertheless I’ll use getVariable and setVariable. Thanks a lot.