Programming Problem: if-statement, local variables and functions

Problem Description:  The program seems to not be able to identify that I’m trying to insert the button into the group when I have defined a ‘filename’ so that it goes to the specific location and retrieves the file, in order to make programming much easier. I understand Lua is Lexical scoped but, it keeps on coming up with errors (which I’ll place in a screenshot below code)

local function newMenuEffect(color) local effect = display.newRect(0,0,98,24) if type(color) == "table" then effect:setFillColor(color[1], color[2], color[3]) else effect:setFillColor(gray) end local function insertGroup(group) group:insert(effect) end local function removeGroup(group) group:remove(effect) end return effect end local function newMenuButton(filename, text) local menuButton = display.newGroup() -- if filename ~= nil then local menuButtonImage = display.newImage(filename) menuButton:insert(menuButtonImage) else local menuButtonImage = display.newImage("Menu/buttonBlue.png") menuButton:insert(menuButtonImage) end -- if text ~= nil then local menuButtonText = display.newText(text, 0, 0, "Courier New", 15 ) menuButton:insert(menuButtonText) end -- local function eventTouch(event) local effect = newMenuEffect() if ( event.phase == "began" ) then effect:insertGroup(event.target) print("DEBUG: Touch event began") elseif ( event.phase == "ended" ) then effect:removeGroup(event.target) effect = nil print("DEBUG: Touch event ended") end return true -- Prevents tap/touch propagation to underlying objects end menuButton:addEventListener("touch", eventTouch) return menuButton end local function mainStart() local menu = display.newGroup() local menuBackground = display.newImage('Menu/menuBackground.png') menu:insert(menuBackground) menu.x = display.contentCenterX menu.y = display.contentCenterY local startGame = newMenuButton("Menu/blueButton.png", "Start Game") local endGame = newMenuButton("Menu/redButton/png", "End Game") menu:insert(startGame) menu:insert(endGame) endgame.y = menubackground.height\*0.4 stargame.y = -(menuBackground.height\*0.3) end mainStart()

Errors:  https://gyazo.com/90d74017be08c72492ef5bbf0c72f5c1

Please in the future, use the console window and copy/paste the text of the error message instead of using a screen shot. It would be really helpful if I could copy and paste the problem for you.

If you look at your screen shot there are three yellow warnings. It can’t find an image file, so display.newImage() in line 24 is not returning the display object, but nil. And in line 25, you’re trying to insert nil into a display group, which you can’t.

Rob

Thank you, I see what you mean - I’ll do so next time… The problem is that when I call the functions and write the parameters:

newMenuButton('menu/blueBackground.png', "Whatever")

It still comes up as an error, is there any specific reason or anything I need to know for such thing?

Are you absolutely sure the file blueButton.png exists?

You seem to default to buttonBlue.png if a filename is not passed - i.e. the words are the opposite way round.

And your test in your second post refers to blueBackground.png, did you mean to put menuBackground.png?

Please in the future, use the console window and copy/paste the text of the error message instead of using a screen shot. It would be really helpful if I could copy and paste the problem for you.

If you look at your screen shot there are three yellow warnings. It can’t find an image file, so display.newImage() in line 24 is not returning the display object, but nil. And in line 25, you’re trying to insert nil into a display group, which you can’t.

Rob

Thank you, I see what you mean - I’ll do so next time… The problem is that when I call the functions and write the parameters:

newMenuButton('menu/blueBackground.png', "Whatever")

It still comes up as an error, is there any specific reason or anything I need to know for such thing?

Are you absolutely sure the file blueButton.png exists?

You seem to default to buttonBlue.png if a filename is not passed - i.e. the words are the opposite way round.

And your test in your second post refers to blueBackground.png, did you mean to put menuBackground.png?