I still don’t understand why I get the error above, but I found a possible workaround and would like to ask if that’s sensitive in terms of memory-usage for example:
I now have a “menubar.lua” module file that contains my buttons, all of which are in a group to allow showing and hiding the menu:
local globalSpace = require("globals") local composer = require( "composer" ) -- table holding that tabbar local tabBar = {} -- set height for tabbar tabBar.menuHeight = 50 -- initialize buttons tabBar.btn1 = nil tabBar.btn2 = nil tabBar.btn3 = nil tabBar.btn4 = nil tabBar.btn5 = nil -- initialize group for buttons tabBar.buttonGroup = nil -- draw line on top of menu tabBar.topline = nil -- initially enable tapping on symbols tabBar.isEnabled = true function tabBar.createMenu() -- options for imageSheet local options = { frames = { { x=0, y=0, width=400, height=400 }, { x=400, y=0, width=400, height=400 }, { x=0, y=400, width=400, height=400 }, { x=400, y=400, width=400, height=400 }, { x=0, y=800, width=400, height=400 }, { x=400, y=800, width=400, height=400 }, { x=0, y=1200, width=400, height=400 }, { x=400, y=1200, width=400, height=400 }, { x=0, y=1600, width=400, height=400 }, { x=400, y=1600, width=400, height=400 } }, sheetContentWidth = 800, sheetContentHeight = 2000 } -- actual imageSheet with menu-symbols on it local tabBarSheet = graphics.newImageSheet( "menuSheet.png", options ) -- group holding the symbol-Sprites tabBar.buttonGroup = display.newGroup() tabBar.topline = display.newRect(display.contentCenterX, display.screenOriginY + display.actualContentHeight - 50, display.contentWidth, 1) tabBar.topline:setFillColor(0.9) tabBar.buttonGroup:insert(tabBar.topline) -- home-button tabBar.btn1 = display.newSprite(tabBar.buttonGroup, tabBarSheet, {name="home", start=1, count=2}) tabBar.btn1:setFrame(1) tabBar.btn1.width = 25 tabBar.btn1.height = 25 tabBar.btn1.x = display.contentWidth \* 0.1 tabBar.btn1.y = display.screenOriginY + display.actualContentHeight - 25 tabBar.btn1:addEventListener("tap", function() if(tabBar.isEnabled == false) then return true end composer.gotoScene("dofaver") end ) -- add-button tabBar.btn2 = display.newSprite(tabBar.buttonGroup, tabBarSheet, {name="add", start=3, count=2}) tabBar.btn2:setFrame(1) tabBar.btn2.width = 25 tabBar.btn2.height = 25 tabBar.btn2.x = display.contentWidth \* 0.3 tabBar.btn2.y = display.screenOriginY + display.actualContentHeight - 25 tabBar.btn2:addEventListener("tap", function() if(tabBar.isEnabled == false) then return true end composer.gotoScene("addFirst") end ) -- faver-button tabBar.btn3 = display.newSprite(tabBar.buttonGroup, tabBarSheet, {name="faver", start=5, count=2}) tabBar.btn3:setFrame(1) tabBar.btn3.width = 25 tabBar.btn3.height = 25 tabBar.btn3.x = display.contentWidth \* 0.5 tabBar.btn3.y = display.screenOriginY + display.actualContentHeight - 25 tabBar.btn3:addEventListener("tap", function() if(tabBar.isEnabled == false) then return true end composer.gotoScene("myfavers") end ) -- search-button tabBar.btn4 = display.newSprite(tabBar.buttonGroup, tabBarSheet, {name="search", start=7, count=2}) tabBar.btn4:setFrame(1) tabBar.btn4.width = 25 tabBar.btn4.height = 25 tabBar.btn4.x = display.contentWidth \* 0.7 tabBar.btn4.y = display.screenOriginY + display.actualContentHeight - 25 tabBar.btn4:addEventListener("tap", function() print("search coming") end ) -- profile-button tabBar.btn5 = display.newSprite(tabBar.buttonGroup, tabBarSheet, {name="profile", start=9, count=2}) tabBar.btn5:setFrame(1) tabBar.btn5.width = 25 tabBar.btn5.height = 25 tabBar.btn5.x = display.contentWidth \* 0.9 tabBar.btn5.y = display.screenOriginY + display.actualContentHeight - 25 tabBar.btn5:addEventListener("tap", function() if(tabBar.isEnabled == false) then return true end composer.gotoScene("setAccount") end ) -- hide menu after creation and show it only when first scene is loaded tabBar.buttonGroup.isVisible = false end -- function to show menu function tabBar.showMenu() tabBar.buttonGroup.isVisible = true end -- function to hide menu function tabBar.hideMenu() tabBar.buttonGroup.isVisible = false end -- function to disable tabbar function tabBar.preventTapping() tabBar.isEnabled = false end -- function to enable tabbar function tabBar.allowTapping() tabBar.isEnabled = true end return tabBar
So, in my main.lua I can call
local tabbar = require(“menubar”)
tabbar.createMenu()
tabbar.showMenu()
to display the menubar. Since it initially resides outside composer scenes, the menubar is always on top of scene-content. But as some of my scenes show overlays which - of course - should be on top of the tabbar, I need to insert the tabbar into the sceneGroups. To do that, I call
sceneGroup:insert(tabbar.buttonGroup)
in scene:show - phase of every scene. If I understand correctly, the tabbar is now both globally available as well as handed from one sceneGroup to the other so that it is always inserted into the currently active scene.
Does that make sense? Is this memory-wise a good solution?
Would appreciate any help here
Thanks,
Chris