Attempt to Index Global variable (a nil value)

menu.lua: 15 attempt to index global btnHelp (a nil value). i make the variable global so that the function onHelpButtonTouch(event) will read my variable btnHelp… i cant figure it…

local function onHelpButtonTouch(event) if "began" == event.phase then btn\_help.isFocus = true print("Button Help is pressed") print(imgTutorial.alpha) if (imgTutorial.alpha == 1) then imgTutorial.alpha = 0 else imgTutorial.alpha = 1 end end return true end local function scene:create(event) btnHelp = display.newImageRect(mainMenu, "images/Buttons/Help", 250, 250) btnHelp.x = 450 btnHelp.y = 1750 imgTutorial = display.newImageRect("images/tutorial", 480, 320) imgTutorial.alpha = 0 btnHelp:addEventListener("touch", onHelpButtonTouch) imgTutorial:addEventListener("touch", onHelpButtonTouch) mainMenu.btnhelp = btnHelp mainMenu.imgTutorial = imgTutorial end

I doubt your image is named “Help”. It’s probably “Help.png” or “Help.jpg”. For display.newImage() and display.newImageRect() you have to specify the full file name including the extension. Modern operating systems like to hide the extension, so this is a common issue.  Also names are case-sensitive when on Device, but not in the simulator, so make sure the case matches on the files and folder names.

Rob

In addition, you don’t need to make the variable global for the reason you describe. 

Put…

[lua]

local btnHelp

[/lua]

…near the top of your lua file. I have a dedicated section where I declare local variables for the scene. Usually this will be a few tables that will store everything I need, so rather than having to decide ahead of time what things will be called, I set up some local tables

[lua]

– TOP OF SCENE

local v = {} – this will contain all my local variables

local gfx = {} – this will contain references to graphics objects

local grp = {} – this wll contain references to display groups

– LATER ON IN SCENE

v.bullets = 4

grp.popup = display.newGroup()

gfx.exitButton = display.newImageRect(grp.popup, “exit.png”, 40, 20)

[/lua]

I doubt your image is named “Help”. It’s probably “Help.png” or “Help.jpg”. For display.newImage() and display.newImageRect() you have to specify the full file name including the extension. Modern operating systems like to hide the extension, so this is a common issue.  Also names are case-sensitive when on Device, but not in the simulator, so make sure the case matches on the files and folder names.

Rob

In addition, you don’t need to make the variable global for the reason you describe. 

Put…

[lua]

local btnHelp

[/lua]

…near the top of your lua file. I have a dedicated section where I declare local variables for the scene. Usually this will be a few tables that will store everything I need, so rather than having to decide ahead of time what things will be called, I set up some local tables

[lua]

– TOP OF SCENE

local v = {} – this will contain all my local variables

local gfx = {} – this will contain references to graphics objects

local grp = {} – this wll contain references to display groups

– LATER ON IN SCENE

v.bullets = 4

grp.popup = display.newGroup()

gfx.exitButton = display.newImageRect(grp.popup, “exit.png”, 40, 20)

[/lua]