Stopping a function

Hi all,

I was wondering if you had any recommendations as to how to stop a function (and be able to restart it later on) on a collision.  By this, I mean that I have objects spawning, and on collision with an incorrect object I would like for it to stop the spawning of the objects.  I saw something about “do return end”, however I am unsure as the function I am referencing is outside the collision code.

Thanks

I suggest posting your code so we can better assist you

Hi, 

Here’s my code:

local function objCollision( self, event ) if event.phase == "began" then if event.other.type == "object1" then if touch == 1 then print( "GAME OVER - OBJECT1 COLLISION") -- I need it to stop the spawn() function here end end if event.other.type == "object2" then if touch == 0 then print( "GAME OVER - OBJECT2 COLLISION") -- I need it to also stop the spawn() function here end end end local function onTouch( event ) if ( event.phase == "began" ) then if startNum == 0 then startNum = 1 spawn() -- spawn begins here end end Runtime:addEventListener( "touch", onTouch ) function spawn() local function spawnObject() -- removed the game logic from here, but this all works fine end timer.performWithDelay( 500, spawnObject, 0 ) end

You can do something like this: 

local tmrSpawn ... local function objCollision( self, event ) if event.phase == "began" then if event.other.type == "object1" then if touch == 1 then print( "GAME OVER - OBJECT1 COLLISION") timer.cancel( tmrSpawn ) end end if event.other.type == "object2" then if touch == 0 then print( "GAME OVER - OBJECT2 COLLISION") timer.cancel( tmrSpawn ) end end end local function onTouch( event ) if ( event.phase == "began" ) then if startNum == 0 then startNum = 1 spawn() -- spawn begins here end end Runtime:addEventListener( "touch", onTouch ) function spawn() local function spawnObject() -- removed the game logic from here, but this all works fine end tmrSpawn = timer.performWithDelay( 500, spawnObject, 0 ) end

There is also timer.pause/timer.resume, depending on what all you need to do.

Thank you so much! I got it all working now :).

Thanks!

I suggest posting your code so we can better assist you

Hi, 

Here’s my code:

local function objCollision( self, event ) if event.phase == "began" then if event.other.type == "object1" then if touch == 1 then print( "GAME OVER - OBJECT1 COLLISION") -- I need it to stop the spawn() function here end end if event.other.type == "object2" then if touch == 0 then print( "GAME OVER - OBJECT2 COLLISION") -- I need it to also stop the spawn() function here end end end local function onTouch( event ) if ( event.phase == "began" ) then if startNum == 0 then startNum = 1 spawn() -- spawn begins here end end Runtime:addEventListener( "touch", onTouch ) function spawn() local function spawnObject() -- removed the game logic from here, but this all works fine end timer.performWithDelay( 500, spawnObject, 0 ) end

You can do something like this: 

local tmrSpawn ... local function objCollision( self, event ) if event.phase == "began" then if event.other.type == "object1" then if touch == 1 then print( "GAME OVER - OBJECT1 COLLISION") timer.cancel( tmrSpawn ) end end if event.other.type == "object2" then if touch == 0 then print( "GAME OVER - OBJECT2 COLLISION") timer.cancel( tmrSpawn ) end end end local function onTouch( event ) if ( event.phase == "began" ) then if startNum == 0 then startNum = 1 spawn() -- spawn begins here end end Runtime:addEventListener( "touch", onTouch ) function spawn() local function spawnObject() -- removed the game logic from here, but this all works fine end tmrSpawn = timer.performWithDelay( 500, spawnObject, 0 ) end

There is also timer.pause/timer.resume, depending on what all you need to do.

Thank you so much! I got it all working now :).

Thanks!