简体   繁体   中英

Attempt to index upvalue which is a nil value

The error "attempt to index upvalue 'askUser'(a nil value)" always shows up, I had to make the statement to a comment to run the app, how do i fix it?

function restartLvl()  
        for i = 1, #balloonTexts do
        display.remove(balloonTexts[i])
        print ("restart level")
    end
    score.text = '0'
    ballRemain.text = '3'
    balloonText = {}
    createBalloons(1, 3)
    if (askUser.isVisible == true) then --this is where the error occured
    askUser.isVisible = false
    end
    if (yesBtn.isVisible == true) then
    yesBtn.isVisible = false
    end
    if (noBtn.isVisible == true) then
    noBtn.isVisible = false
    end

    print("time from start: ", (system.getTimer()-gameTime))
    print('send mail')
    sendMail()
    restartBtn:removeEventListener('tap', restartLvl)

end

The message is telling you that askUser is a local variable defined outside restartLvl and that askUser is nil and so cannot be indexed.

You'll have to find out why askUser is nil when you don't expect it to be.

You need to give askUser.isVisible a value. Example:

askUser = blablabla
askUser.x = blablabla
aksUser.y = bablabla

askUser.isVisible = false

group:insert(askUser)

PS: Didn't know that there is an isVisible var :P I've always used askUser.alpha = 0

Before removing your object from view check for nil

for example

 local try    --object on which you are working
    if(try==nil)then
       --do nothing
   else
      --do here what u want to do
   end

I was also getting same problem, in my case this is code working.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM