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:
- Set up and display a title screen along with a “start” button.
- 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.
- Then loadGame wil call my Graphics_Initialization function to set up the player and enemy, etc.
- 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]