"Attempt to index upvalue" is driving me crazy

Hello all,
 
I am just starting at (as will be obvious to most of you) and I have hit a snag.
 
My code is supposed to do the following:
 

  1. Set up and display a title screen along with a “start” button.
  2. Upon tapping the button, it should now go to another section called loadGame to perform some housekeeping and transition from the title screen to the start screen.
  3. Then loadGame wil call my Graphics_Initialization function to set up the player and enemy, etc.
  4. Then the movePlayer function kicks in and allows the user to drag the player image around.
     
    So, when the program starts, the title screen and button come up. Upon clicking the button, I am met with the following error:
     
    “Attempt to index upvalue ‘Player’ (a nil value)”
     
     
    stack traceback:

    [C]: ?

    …mes/Programming/Corona Programming/PlayTIme/main.lua:119: in function <…mes/Programming/Corona Programming/PlayTIme/main.lua:113>

    ?: in function <?:218>
 
In attempt to seek out my own answer, I stumbled upon “widgets”. Now, if I use the widget code (which is commented out), everything works fine. When I attempt to use the bitmap button, that is, the “startButton” code, I get the error above.
 
Can someone please point me in the right direction? I’ve struggled with this problem for a few hours (yes, I am a newbie!) before turning to this forum for help. I’ve pasted my code in below…
 
Thank you all in advance.
 
-Spo-
 
==BEGIN==

[lua]–
– main.lua



– Get pixel width and height of device you are running on –

local Screen_Width = display.pixelWidth
local Screen_Height = display.pixelHeight

local titleScreenGroup
local titleScreen
local startButton
local Player – I’ve tried with and without this declaration
local Player_StartX
local widget = require (“widget”)

– Variables to control flow of game –

local Game_Over = 0

----------------------------------------FUNCTION SECTION----------------------------------

-----------------------------------INITIALIZE GRAPHICS------------------------------------


– This section is where we load the graphic images used in our game and set their –
– initial positions. –



function Show_Title_Screen()
display.setStatusBar(display.HiddenStatusBar)
titleScreenGroup = display.newGroup()

titleScreen = display.newImage(“titlescreen.png”, 0, 0, true)
titleScreen.x = (display.contentWidth / 2)
titleScreen.y = (display.contentHeight / 2)

– startButton = widget.newButton
– {
– left = 60,
– top = Screen_Height / 2,
– label = “Start”,
– labelAlign = “center”,
– font = “Arial”,
– fontSize = 18,
– labelColor = { default = {0,0,0}, over = {255, 255, 255} },
– onEvent = onButtonEvent
– }

startButton = display.newImage(“startbutton.png”)
startButton.x = Screen_Width / 2
startButton.y = Screen_Height - (Screen_Height / 2)
startButton.name = “startButton”

titleScreenGroup:insert(titleScreen)
titleScreenGroup:insert(startButton)

startButton:addEventListener(“tap”, loadGame);

end

function loadGame(event)
if event.target.name == “startbutton” then
transition.to(titleScreenGroup, { time = 500, alpha = 0, onComplete = Graphics_Initialization});
startButton:removeEventListener(“tap”, loadGame)
end
end

--------------------- THE BELOW CODE WORKS FOR WIDGETS ------

–function loadGame(onEvent)
– if onEvent.target == startButton then
– transition.to(titleScreenGroup, { time = 500, alpha = 0, onComplete = Graphics_Initialization});
– startButton:removeEventListener(“tap”, loadGame)
– end
– end

function Graphics_Initialization ()

– Player Set-Up –
Player = display.newImage (“crate.png”)
Player.x = 100
Player.y = (Screen_Height - Player.contentHeight * 2)
Player.name = “player”

– Background Set-Up –

end

function loadGame(event)
if event.target.name == “startbutton” then
transition.to(titleScreenGroup, { time = 500, alpha = 0, onComplete = Graphics_Initialization});
startButton:removeEventListener(“tap”, loadGame)
end
end

function movePlayer (event)
local phase = event.phase
–local Player_StartX
–local Player_StartY

if “began” == phase then
Player_StartX = event.x - Player.x
–Player_StartY = event.y - Player.y
else if “moved” == phase then
Player.x = event.x - Player_StartX
if Player.x < (Player.contentWidth *.5) then
Player.x = (Player.contentWidth * .5) end
if Player.x > (Screen_Width - (Player.contentWidth *.5)) then
Player.x = (Screen_Width - (Player.contentWidth *.5)) end

–Player.y = event.y - Player_StartY

end
end

end

function main ()
Show_Title_Screen()
–Graphics_Initialization()
Runtime:addEventListener(“touch”, movePlayer)

end

main()
[/lua]

that error is almost always an indication that something is out of scope

or something should be in a different order

what line is 119

I believe it is the following, as I am not in front of my computer at the moment:

if “began” == phase then
Player_StartX = event.x - Player.x

Thank you so much for taking the time out to help me.

move this line

Runtime:addEventListener(“touch”, movePlayer)

as last line of

Graphics_Initialization function

what happening is that line of code starts running before player is created so the app doesnt know what player is

I’ll give it a shot. What I don’t understand is that if I am reading my code right, I am not calling that event listener until after Player is created. My logic firsts sets up the title screen, then the transition to the graphic routine which sets up the player. It shouldn’t be getting to the movePlayer until after my graphics are set up. Am I missing something about event handlers and Corona SDK?

Thank you again.

Graphics_Initialization function isnt called until after the transition.to in loadGame

Runtime is called right after Show_Title_Screen() is called

Gotcha.

If I can have just one more moment of your time…

  1. After I move it, will I have to call it again in my main program loop or will it continue to function?
    2).Why does it work fine when I use the button widget?

Thanks again. As soon as I get home from work I will try it and post the results!

  1. No once started it continues until removed
  2. Idk maybe when you get home make it work with widget then post that code I know it’s the same as above but maybe something was different

Here is the working widget code:

[lua]



– main.lua



– Get pixel width and height of device you are running on –

local Screen_Width = display.pixelWidth
local Screen_Height = display.pixelHeight

local titleScreenGroup
local titleScreen
local startButton
–local Player – I’ve tried with and without this declaration
–local Player_StartX
local widget = require (“widget”)

– Variables to control flow of game –

local Game_Over = 0

----------------------------------------FUNCTION SECTION----------------------------------

-----------------------------------INITIALIZE GRAPHICS------------------------------------


– This section is where we load the graphic images used in our game and set their –
– initial positions. –



function Show_Title_Screen()
display.setStatusBar(display.HiddenStatusBar)
titleScreenGroup = display.newGroup()

titleScreen = display.newImage(“titlescreen.png”, 0, 0, true)
titleScreen.x = (display.contentWidth / 2)
titleScreen.y = (display.contentHeight / 2)

startButton = widget.newButton
{
left = 60,
top = Screen_Height / 2,
label = “Start”,
labelAlign = “center”,
font = “Arial”,
fontSize = 18,
labelColor = { default = {0,0,0}, over = {255, 255, 255} },
onEvent = onButtonEvent
}

– startButton = display.newImage(“startbutton.png”)
– startButton.x = Screen_Width / 2
– startButton.y = Screen_Height - (Screen_Height / 2)
– startButton.name = “startButton”

titleScreenGroup:insert(titleScreen)
titleScreenGroup:insert(startButton)

startButton:addEventListener(“tap”, loadGame);

end

–function loadGame(event)
– if event.target.name == “startbutton” then
– transition.to(titleScreenGroup, { time = 500, alpha = 0, onComplete = Graphics_Initialization});
– startButton:removeEventListener(“tap”, loadGame)
– end
–end

--------------------- THE BELOW CODE WORKS FOR WIDGETS ------

function loadGame(onEvent)
if onEvent.target == startButton then
transition.to(titleScreenGroup, {time = 500, alpha=0,
onComplete = Graphics_Initialization});
startButton:removeEventListener(“tap”, loadGame)
end
end

function Graphics_Initialization ()

– Player Set-Up –
Player = display.newImage (“crate.png”)
Player.x = 100
Player.y = (Screen_Height - Player.contentHeight * 2)
Player.name = “player”

– Background Set-Up –

end

function movePlayer (event)
local phase = event.phase
–local Player_StartX
–local Player_StartY

if “began” == phase then
Player_StartX = event.x - Player.x
–Player_StartY = event.y - Player.y
else if “moved” == phase then
Player.x = event.x - Player_StartX
if Player.x < (Player.contentWidth *.5) then
Player.x = (Player.contentWidth * .5) end
if Player.x > (Screen_Width - (Player.contentWidth *.5)) then
Player.x = (Screen_Width - (Player.contentWidth *.5)) end

–Player.y = event.y - Player_StartY

end
end

end

function main ()
Show_Title_Screen()
–Graphics_Initialization()
Runtime:addEventListener(“touch”, movePlayer)

end

main()
[/lua]

I moved the line to the end of the Graphics_Initialization function. It worked like a charm.

Thank you so much for your help. I really appreciate it.

-Spo-

welcome

as to why it doesnt work when using an image is cause of the transition.to more then likely

as to why it worked with widget idk cause the transition.to is still there, also the use of widget is incoreect

the onEvent option should be calling loadGame() and you shouldn’t have the eventListener added to the button

that error is almost always an indication that something is out of scope

or something should be in a different order

what line is 119

I believe it is the following, as I am not in front of my computer at the moment:

if “began” == phase then
Player_StartX = event.x - Player.x

Thank you so much for taking the time out to help me.

move this line

Runtime:addEventListener(“touch”, movePlayer)

as last line of

Graphics_Initialization function

what happening is that line of code starts running before player is created so the app doesnt know what player is

I’ll give it a shot. What I don’t understand is that if I am reading my code right, I am not calling that event listener until after Player is created. My logic firsts sets up the title screen, then the transition to the graphic routine which sets up the player. It shouldn’t be getting to the movePlayer until after my graphics are set up. Am I missing something about event handlers and Corona SDK?

Thank you again.

Graphics_Initialization function isnt called until after the transition.to in loadGame

Runtime is called right after Show_Title_Screen() is called

Gotcha.

If I can have just one more moment of your time…

  1. After I move it, will I have to call it again in my main program loop or will it continue to function?
    2).Why does it work fine when I use the button widget?

Thanks again. As soon as I get home from work I will try it and post the results!

  1. No once started it continues until removed
  2. Idk maybe when you get home make it work with widget then post that code I know it’s the same as above but maybe something was different