"Stopping" a function. Help with game logic

I have this code (posted below) to allow the user to fling skulls and everything works great.  The issue is when the user loses the game.  The game ends as it should if the user isn’t in the process of throwing.

So when I play the game and don’t touch the screen, I can lose.  When I play the game as I should but I’m hit in between throws, I can lose.  However, if I’m touching the screen and hit, the game over function won’t call. It’s like it just ignores it and then if I’m hit again while I’m not touching the screen the game over function will call. It’s weird. 

Just looking for a little guidance to push me in the right direction.

[lua] local function skullThrow(event)
if event.phase == ‘began’ then
display.getCurrentStage():setFocus(skulls[skullNum])
brody[1].isVisible = false
brody[2].isVisible = true
return true
elseif event.phase == ‘moved’ then
brody[2].isVisible = false
brody[3].isVisible = true
elseif event.phase == ‘ended’ then
skulls[skullNum].isVisible = true
brody[3].isVisible = false
brody[4].isVisible = true
skulls[skullNum].isBodyActive = true
skulls[skullNum]:applyLinearImpulse(event.xStart - event.x, event.yStart - event.y, skulls[skullNum].x, skulls[skullNum].y)
skulls[skullNum]:applyTorque(math.random(0,500))
timer.performWithDelay(40, spawnSkull)
return false
end
end[/lua]

Here’s the code for the game over function

[lua] gameOver = function()
timer.cancel(spawn1)
if score > 2500 then
timer.cancel(spawn2)
end
print('Ending score is '…score)
if score > highScore then
highScore = score
_path = system.pathForFile( “highScore.txt”, system.DocumentsDirectory )
_file = io.open( _path, “w” )
_file:write( highScore )

io.close( _file )
_file = nil
_path = nil
end
composer.gotoScene(‘mainmenu’)
for p = 1, table.maxn(skulls) do
skulls[p]:removeEventListener(‘touch’, skullThrow)
Runtime:removeEventListener(‘enterFrame’, gameLoop)
end
end[/lua]

I’m not sure why there aren’t any indents in the code but it’s fairly straightforward lol

It looks like you only do “return true” in one path in your touch listener, so in the others you’re falling through and touching whatever is beneath (but not during “began”, so in some inconsistent halfway state). You are setting the focus (are you unsetting it, when done?), but there may be some weird interaction going on all the same.

This is just an educated guess, of course, without seeing more.

I haven’t unset the focus but I’ll try that right now.  You think the return statements are causing this?

The code is grabbing an image that the character (brody[#]) is holding so when you’re touching the screen the .isVisible is animating him ((in a sort of awkward manner but I wasn’t sure how to scale sprites to different screens ))

Thanks StarCrunch! I unset the focus and added return statements and it fixed the problem!!!   :lol:  :lol:

EDIT:   I lied… On some runs of the game it’ll work perfectly but on others it’ll take being hit 3 or more times for the game to end.  I’ll keep playing around with it though. 

Great to hear!  :slight_smile:

It looks like you only do “return true” in one path in your touch listener, so in the others you’re falling through and touching whatever is beneath (but not during “began”, so in some inconsistent halfway state). You are setting the focus (are you unsetting it, when done?), but there may be some weird interaction going on all the same.

This is just an educated guess, of course, without seeing more.

I haven’t unset the focus but I’ll try that right now.  You think the return statements are causing this?

The code is grabbing an image that the character (brody[#]) is holding so when you’re touching the screen the .isVisible is animating him ((in a sort of awkward manner but I wasn’t sure how to scale sprites to different screens ))

Thanks StarCrunch! I unset the focus and added return statements and it fixed the problem!!!   :lol:  :lol:

EDIT:   I lied… On some runs of the game it’ll work perfectly but on others it’ll take being hit 3 or more times for the game to end.  I’ll keep playing around with it though. 

Great to hear!  :slight_smile: