Reputation: 1
I'm fairly new to Lua, and one particular command in my code has been causing me some major problems.
I've been trying to cancel the timer:
currentPuddleDelay = timer.performWithDelay(puddleDelay, createPuddle);
The error that I am shown is:
Attempt to index a nil value
File: ?
stack traceback:
?: in function 'cancel'
game.lua:534: in function '?'
?: in function 'dispatchEvent'
?: in function '_saveSceneAndHide'
?: in function 'gotoScene'
game.lua:452: in function '?'
?: in function <?:182>
From what I've researched already, this problem can occur when the timer is within a function and is local, however, the timer in my code is global, so I don't think that that is the problem.
Below is the bit of code with the issue:
local function createPuddle()
local function constantDelay()
local puddle = display.newImage( sceneGroup, "images/puddle.png" )
puddle.x = puddleLane
puddle.y = -200
physics.addBody( puddle, "dynamic", {density=0, filter=puddleCollisionFilter} )
puddle:applyForce( 0, puddleSpeed, puddle.x, puddle.y )
sceneGroup:insert(3,puddle)
local function onPuddleCollision( self, event )
if ( event.phase == "began" ) then
print('puddle collision')
puddle:removeSelf( )
puddle = nil
composer.gotoScene( "menu" )
end
end
puddle.collision = onPuddleCollision
puddle:addEventListener( "collision" )
end
local constantDelayTimer = timer.performWithDelay(puddleDelay/2,constantDelay,1)
currentPuddleDelayHold = timer.performWithDelay(puddleDelay, createPuddle);
end
currentPuddleDelay = timer.performWithDelay(puddleDelay, createPuddle);
And then later on in the program:
timer.cancel(currentPuddleDelay)
Any help would be greatly appreciated.
Upvotes: 0
Views: 637
Reputation: 28974
I can only guess as you most likely did not provide all relevant code.
It obviously doesn't make sense to cancel a non-existing timer so for the start just do
if currentPuddleDelay then timer.cancel(currentPuddleDelay) end
If there is any reason why currentPuddleDelay should still exist you should find out why it is nil.
Upvotes: 1