I am working on a jumper game to practice my lua programming and I have come across a problem. I created a function to determine what will happen when the character is hit by an enemy. However, it gives me an “Attempt to index field ‘?’ a nil value” error pointing to Line 269. This only happens if the enemy is hit at a certain point on its body, and I am not sure why. Otherwise, it works fine.
local function playerHit()
character.x = _L + 250
character.alpha = 1
lives[livesCount].alpha = 0 --Line 269
livesCount = livesCount - 1
if(livesCount <= 0) then
onGameOver()
end
end
I think it might be referring to lives[livesCount] as nil, but I defined it in my forward declares and defined its graphics in a for loop way before this function:
– forward declarations and other locals
local lives = {} – table that will hold the lives object
local livesCount = 4 – the number of lives the player has
local function playerHit() character.x = \_L + 250 character.alpha = 1 lives[livesCount].alpha = 0 --Line 269 livesCount = livesCount - 1 if(livesCount \<= 0) then onGameOver() end end
If livesCount is equal to 0 you’re going to try and access lives[0] which doesn’t exist. You probably should be testing if livesCount < 1 then calling onGameOver().
Thank you for your quick reply, but this is occurs when the user has all of their lives. Is it possible to split playerHit() in to two different functions, and run them both?
local function showPlayerHit()
character.alpha = 0.5
local tmr_onPlayerHit = timer.performWithDelay(1, playerHit, 1)
--add second timer
end
if(event.object1.id == “enemy” and event.object2.id == “character”) then
showPlayerHit()
removeOnPlayerHit(event.object1, nil)
end
if(event.object1.id == “character” and event.object2.id == “enemy”) then
I would put a print statement before the line that errors out and find out what it thinks livesCount is. The error is basically saying that lives[livesCount] is not a table object. The most likely cause is that livesCount isn’t what you think it is and lives[livesCount] is nil
local function playerHit() character.x = \_L + 250 character.alpha = 1 lives[livesCount].alpha = 0 --Line 269 livesCount = livesCount - 1 if(livesCount \<= 0) then onGameOver() end end
If livesCount is equal to 0 you’re going to try and access lives[0] which doesn’t exist. You probably should be testing if livesCount < 1 then calling onGameOver().
Thank you for your quick reply, but this is occurs when the user has all of their lives. Is it possible to split playerHit() in to two different functions, and run them both?
local function showPlayerHit()
character.alpha = 0.5
local tmr_onPlayerHit = timer.performWithDelay(1, playerHit, 1)
--add second timer
end
if(event.object1.id == “enemy” and event.object2.id == “character”) then
showPlayerHit()
removeOnPlayerHit(event.object1, nil)
end
if(event.object1.id == “character” and event.object2.id == “enemy”) then
I would put a print statement before the line that errors out and find out what it thinks livesCount is. The error is basically saying that lives[livesCount] is not a table object. The most likely cause is that livesCount isn’t what you think it is and lives[livesCount] is nil