is it possible to keep a widget on multiple scenes?

i want to have a quit button on my level1.lua,level2.lua…   but i dont want to have to copy and paste the same code in each lua file. I want to know if i can keep the widget button in another file and access it when needed.

I would just put it inside a module like this

------myWidget.lua
local m = {}
return m
----main.lua
local myWidget = require(“myWidget”)
myWidget.widget1 = display.newRect (0,0,50,50)
myWidget.widget1.alpha = 0
------level1.lua
local myWidget = require(“myWidget”)
myWidget.widget1.alpha = 1
-------level2.lua
local myWidget = require(“myWidget”)
myWidget.widget1.alpha = .5uuvyngiii

Sorry if this is teaching the sucking of eggs, but…

Absolutely, the key to software development is to reuse code as much as possible. Programming languages give you all sorts of mechanisms to reuse code and Lua uses modules. That is, each thing you want to reuse goes into a separate Lua file and you load that with the require command. Load the module in your scene and use a function in that module to create new instances of it.

Take a look at lots of the example code and the module files which come with them. You will find that each module has a function which can return an object to be used in a scene or display group. Structure your code like that.

You can also look in the Code Exchange to see how other people do it.

Also a feature of the Composer scene manager is if you do not put your object in the composer’s view group, it will stay on top and not be managed. It’s what we call HUD mode.

Rob

rob, if i do that, how would i remove it on certain scenes. 

if i create a back button in level1, its gonna show for level2,level3,level4… but when i go to the main page, how can i remove it.

thx for your help guys

My advice for managing HUD objects is to create a module which gives you a display group which can be used to add and remove HUD objects. This would be passed as a parameter to any scene, or simply loaded in each scene module and is able to return the same group each time.

If you create a back button, do you really need it to be different for each level? If you really do, can it be made customisable by each level? Eg: If it has text in it, change the text when a level loads.

My HUD management module (which I don’t use because I don’t use a HUD, so this is off the top of my head) would be something like:

-- HUD local composer = require("composer") local hud local function getHUD() if (hud) then return hud end hud = display.newGroup() group.class = "hud" -- call this function in your scene's Show.Will phase function hud:ensureHudOnTop() hud:toFront() end return hud end return getHUD

Hide it with .isVisible = false. You would need to setup a “global” like reference to it in each scene that you wanted to show/hide it from (easiest is: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/)..)

@Horacebury’s suggestion is also a fantastic option.

I would just put it inside a module like this

------myWidget.lua
local m = {}
return m
----main.lua
local myWidget = require(“myWidget”)
myWidget.widget1 = display.newRect (0,0,50,50)
myWidget.widget1.alpha = 0
------level1.lua
local myWidget = require(“myWidget”)
myWidget.widget1.alpha = 1
-------level2.lua
local myWidget = require(“myWidget”)
myWidget.widget1.alpha = .5uuvyngiii

Sorry if this is teaching the sucking of eggs, but…

Absolutely, the key to software development is to reuse code as much as possible. Programming languages give you all sorts of mechanisms to reuse code and Lua uses modules. That is, each thing you want to reuse goes into a separate Lua file and you load that with the require command. Load the module in your scene and use a function in that module to create new instances of it.

Take a look at lots of the example code and the module files which come with them. You will find that each module has a function which can return an object to be used in a scene or display group. Structure your code like that.

You can also look in the Code Exchange to see how other people do it.

Also a feature of the Composer scene manager is if you do not put your object in the composer’s view group, it will stay on top and not be managed. It’s what we call HUD mode.

Rob

rob, if i do that, how would i remove it on certain scenes. 

if i create a back button in level1, its gonna show for level2,level3,level4… but when i go to the main page, how can i remove it.

thx for your help guys

My advice for managing HUD objects is to create a module which gives you a display group which can be used to add and remove HUD objects. This would be passed as a parameter to any scene, or simply loaded in each scene module and is able to return the same group each time.

If you create a back button, do you really need it to be different for each level? If you really do, can it be made customisable by each level? Eg: If it has text in it, change the text when a level loads.

My HUD management module (which I don’t use because I don’t use a HUD, so this is off the top of my head) would be something like:

-- HUD local composer = require("composer") local hud local function getHUD() if (hud) then return hud end hud = display.newGroup() group.class = "hud" -- call this function in your scene's Show.Will phase function hud:ensureHudOnTop() hud:toFront() end return hud end return getHUD

Hide it with .isVisible = false. You would need to setup a “global” like reference to it in each scene that you wanted to show/hide it from (easiest is: http://coronalabs.com/blog/2013/05/28/tutorial-goodbye-globals/)..)

@Horacebury’s suggestion is also a fantastic option.