Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Attempt to index field '?' a nil value error
Started by Blex Jul 18 2016 01:27 PM

- - - - -
3 replies to this topic
lua
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Blex

[GLOBAL: userInfoPane.html]
Blex
  • Corona Geek

  • 1,199 posts
  • Corona SDK

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.
 
 
 
 


[TOPIC: post.html]
#2

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,394 posts
  • Enterprise

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().

 

Rob



[TOPIC: post.html]
#3

Blex

[GLOBAL: userInfoPane.html]
Blex
  • Corona Geek

  • 1,199 posts
  • Corona SDK

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
                showPlayerHit()
                removeOnPlayerHit(nil, event.object2)
            end
 
Thank you.
 
Sincerely,
Alex


[TOPIC: post.html]
#4

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,394 posts
  • Enterprise

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

 

So finding out whats there is important.

 

Rob




[topic_controls]
[/topic_controls]