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
