attempt to index a nil value?

Hello!

i have a pause menu that works well the only error now is that then i resume my game it tells me:

Runtime error
?:0: attempt to index a nil value
stack traceback:
[C]: ?
?: in function ‘cancel’
…sx7vhhg96sjffvw0000gn/T/TemporaryItems/7/level01.lua:184: in function <…sx7vhhg96sjffvw0000gn>
?: in function <?:215>

i add the code associated with this problem under and hope that someone know the problem? i mean these functions shouldn’t even get executed because they are associated with the big button under the pause menu that aint related to the pause menu?
[lua]–A function the controls the ufos movement
function ufoMovement(event)


--An if that controls what happens then user hit anywhere on the screen and it goes up
if(event.target.name == “up” and crashed == false) then

--An if that fires an timer then touch began
if(event.phase == “began”) then

tmrUp = timer.performWithDelay(150, engines, 0)


--An elseif that checks then user stops to touch the button
elseif(event.phase == “ended”) then
timer.cancel(tmrUp)


spriteInstance.linearDamping = 0

--An elseif that checks if ufo has crashed then makes spriteInstance the crashedPos
elseif(crashed == true) then

spriteInstance.x = crashedPos

end

end

--An if that controls what happens then user hit the left button and it goes left
if(event.target.name == “left” and crashed == false) then

--An if that fires an timer then touch began
if(event.phase == “began”) then

tmrLeft = timer.performWithDelay(200, sideleft, 0)

--An elseif that checks then user stops to touch the button
elseif(event.phase == “ended”) then

timer.cancel(tmrLeft)

--An elseif that checks if ufo has crashed then makes spriteInstance the crashedPos
elseif(crashed == true) then

spriteInstance.x = crashedPos

end

end

--An if that controls what happens then user hit the right button and it goes right
if(event.target.name == “right” and crashed == false) then

--An if that fires an timer then touch began
if(event.phase == “began”) then

tmrRight = timer.performWithDelay(150, sideright, 0)

elseif(event.phase == “ended”) then

timer.cancel(tmrRight)

--An elseif that checks if ufo has crashed then makes spriteInstance the crashedPos
elseif(crashed == true) then

spriteInstance.x = crashedPos

end

end

end


–A function with a timer in that applys force to the sprite up
function engines(event)


spriteInstance.linearDamping = 0.5
spriteInstance:applyForce(0, -30, spriteInstance.x, spriteInstance.y)


end

–A function with a timer in that applys force to the sprite left
function sideleft(event)

spriteInstance:applyForce(-20, 0, spriteInstance.x, spriteInstance.y)

end

–A function with a timer in that applys force to the sprite right
function sideright(event)

spriteInstance:applyForce(20, 0, spriteInstance.x, spriteInstance.y)

end


–Three listeners that listen for touch on the different buttons in the game
buttonUp:addEventListener(“touch”, ufoMovement)
buttonLeft:addEventListener(“touch”, ufoMovement)
buttonRight:addEventListener(“touch”, ufoMovement)[/lua]

Kind Regards

Inkoqnito [import]uid: 90942 topic_id: 16847 reply_id: 316847[/import] </…sx7vhhg96sjffvw0000gn>

Hi.

Predeclare your tmrUp variable.

Above your function

local tmrUp  

in your function

tmrUp = timer.performWithDelay(150, engines, 0)  

That way your timer’s scope is broadened so you can access it outside of the function (which you are currently declaring inside the function, so the timer is local and accessible only to that function, hence the error) [import]uid: 84637 topic_id: 16847 reply_id: 63126[/import]

Hey Danny!

I put local tmrUp above function ufoMovement but i STILL got the same error:S

what am i doing wrong?

*Edit ok very strange now it works but i only put tmrUp above the function why dont i get errors on tmrright and tmrLeft they are still just in the function!?

Confusing:P

*Edit2 Sorry for so many edits but now i got the error again:S Can it be something wrong in my other code? Or is it a bug in corona simulator?

/Inkoqnito [import]uid: 90942 topic_id: 16847 reply_id: 63134[/import]

Making a post to clarify, i now noticed that the error only happens if i pause the game before i have touched the up button, but if i touch the up button first and then pause and resume the error never appears!?

/Inkoqnito [import]uid: 90942 topic_id: 16847 reply_id: 63137[/import]

I would put the references to the other timers either at the start of the function or above it also. It reduces the risk of scope errors :slight_smile: [import]uid: 84637 topic_id: 16847 reply_id: 63138[/import]

Hi Danny!

Okey will do it, but still get the same error if i pause before touched the up button, do you know the problem or should just ignore it? the game works despite the error.

Kind regards

Inkoqnito [import]uid: 90942 topic_id: 16847 reply_id: 63140[/import]

I alway declare variable that don’t need to be local to a certain function at the top of the code: variables, function names, image names, and sound names then set the values later in the code as I need to [import]uid: 7911 topic_id: 16847 reply_id: 63149[/import]

Hi jstrahan!

I have declared my timers earlier now but every time i pause the game before touching the up button first i get the error then i touch the up button after the game is resumed.

Do you have any idea why?

It fells try strange, the game works and all but irritating with the error:(

i dont declare any variables or other things inside the function anymore!

Kind regards Inkoqnito [import]uid: 90942 topic_id: 16847 reply_id: 63155[/import]

how are you pausing [import]uid: 7911 topic_id: 16847 reply_id: 63167[/import]

i use directors popup, first i have

[lua]function pauseGame(event)

if(event.target.name == “pause”)then

physics.pause()
director:openPopUp(“options”, closed)

end

return true

end

pause:addEventListener(“touch”, pauseGame)[/lua]
And then in the pause menu lua file i have:

[lua] function menuTouch(event)

–An if that fires an timer then touch began
if(event.phase == “began”) then

director:closePopUp()
physics.start()
physics.setGravity(0,5,0)

end

return true

end
options:addEventListener(“touch”, menuTouch)[/lua]

i am not sure about how i should treat physics but it seems to work except the error about nil

/Inkoqnito [import]uid: 90942 topic_id: 16847 reply_id: 63195[/import]

is the error msg the same as OP or is it different now
the error in the OP says the error occured at line 184 but the code doesnt show us the code around line 184 [import]uid: 7911 topic_id: 16847 reply_id: 63205[/import]

its because i didn’t posted the whole lua file, the error occurs in line 16 in the code i posted, the lua file is over 200 lines so i just posted the functions affected [import]uid: 90942 topic_id: 16847 reply_id: 63211[/import]

Hey everyone!

I have finally come up with a solution, i still don’t know why that function tries to execute or rather that the timer.cancel complains on nil, but i just added :

[lua]if(tmrUp ~= nil)then

timer.cancel(tmrUp)

end[/lua]

And now everything works perfect if someone can understand why ufoMovement seems to react then i touch pauseMenu please speak your mind:)

But this way i can continue without any errors anyway:)
Thanks all

/Inkoqnito [import]uid: 90942 topic_id: 16847 reply_id: 63362[/import]