How do you establish gameover overlay when an event indicating player died is triggered?

I’ve put in hours of research and programming to fix this specific problem to no avail so I’m seeking advice.

The game has a defense game where player gets three lives. If all the lives are exhausted, then an overlay should take immediate effect to indicate the player that it’s game over. There are two modules that are in play with the sequence: game and life modules. 

The game module is where the overlay takes place once it receives an action indicating that the player died, and that information comes from the life module which contains the following function to track lives:

local H = {} local w = display.contentWidth H.lives = 3 return function H.loselife() H.lives = H.lives - 1 if H.lives \>= 1 then H.bars[1]:removeSelf() elseif H.lives == 0 then H.bars:removeSelf() end return H.lives end return H

Now this module is imported in the game module prior to composer scene methods. I have attempted a myriad of approach to establish communication between the two modules such that gameover is called when lives are exhausted. So, I won’t add the code demonstrating that since there’s simply too many I tried, but I will basically explain gist of some attempts:

  1. Correct me whether this is valid, but I figured this could be handled with a custom event handler. So, I considered creating a scene method scene:gameover( event ) in the game module, and have it listen to a custom event dispatcher which is sent from life module. I tried this theory with various implementation of this theory, but I simply don’t know how to go from the dispatcher to the scene method.

  2. I considered putting the game over overlay in the life.lua but I fear that it would compromise the purpose of the life module since it would require composer, but I don’t want the game module to involve composer.

Any suggestions would be greatly appreciated.

  • Dan.
     

Hey Dan,

I’m not quite sure what excactly your problem is.

I mean, your loselife function already returns the current lifes (should be written with an “f” not with a “v”, but that’s irrelevant), so it should be easy to check from outside, if a gameover occurred.

All you have to do, is to require your life module in your game module and check for the returned value after you used the looselife functions.

If I’m missing the point, please clarify your question, so I can try to help you.

Greetings

Torben :slight_smile:

Hey Dan,

I’m not quite sure what excactly your problem is.

I mean, your loselife function already returns the current lifes (should be written with an “f” not with a “v”, but that’s irrelevant), so it should be easy to check from outside, if a gameover occurred.

All you have to do, is to require your life module in your game module and check for the returned value after you used the looselife functions.

If I’m missing the point, please clarify your question, so I can try to help you.

Greetings

Torben :slight_smile: