I’m trying to properly remove objects when changing scenes. I can’t get my button to work because the function and button are local. When I press the tutBtn, I get an error to set the tutBtn to global, notice line 30, well I don’t want to set it to a global. Yet, if I put the button before the function, the function obviously won’t recognize the button and nothing will happen. Also, if I make a createScene() function after setting my variables, it’ll actually skip all the way to my target screen, “tutMenu”, and skip the main menu all together.
module(..., package.seeall) function new() local menuGroup = display.newGroup() local widget = require "widget" local bg local title local tutBtn local bg = display.newRect(0,0, display.contentWidth,display.contentHeight) bg:setReferencePoint(display.CenterReferencePoint) bg:setFillColor(245,245,245) menuGroup:insert(bg) local title = display.newImageRect( "title.png", 480, 164) title:setReferencePoint(display.CenterReferencePoint) title.x = display.contentCenterX title.y = 75 menuGroup:insert(title) local function toTutorials( event ) if event.phase == "ended" then bg:removeSelf();bg = nil title:removeSelf();title = nil tutBtn:removeSelf();tutBtn = nil director:changeScene("tutMenu", "crossfade") end end local tutBtn = widget.newButton { -- left = 10, -- top = 200, width = 321, height = 87, defaultFile = "tutBtn.png", overFile = "tutBtn-over.png", id = "tutBtn", onEvent = toTutorials, } tutBtn.x = display.contentWidth/2 tutBtn.y = 200 menuGroup:insert(tutBtn) return menuGroup end