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
for i=1, livesCount do
lives[i] = display.newImageRect(“heart.png”, 50, 50)
lives[i].x = _L + (i * 65) - 25
lives[i].y = _T + 50
end
If anybody has a solution to this, it would be greatly appreciated.