Jump to content

In the "Making your first Phaser game" tutorial, how do I use a Signal for the score?


Recommended Posts



In my update function I wrote

if(score > 100){

    // You win!




This is obviously going to run way too many times to be useful. So I need to know how to watch the score and end the game. A user mentioned Signals but I have no idea where to use them.


Or am I going about this the entire wrong way?

Link to comment
Share on other sites

Basic premise would be that whatever function updates your score variable also updates the UI. This is naive but extremely easy.

So, with that premise you now need a way to call that function.

Either declare the function as global (same as sticking it on Game, I think that is available everywhere with Phaser) and then, whenever you do something that grants score then call the function. Your UI should use your score value i.e. have one single point of truth.

Another way (but slightly more complex) is to use events, or signals. Signals implement the pub/sub pattern (research if you dont know what that is), so, subscribe your function to a signal, and then, whenever your game does something that grants score, publish that event, the listener will fire and update the score.

edit: Sorry, I read your question wrong. Same basic thing though, whenever something grants a score, have your function check if the score now goes over 100 and if it does then change your game state accordingly, probably you'll want to do something that moves you in to the YOU WIN, YOU ARE AWESOME state.

Link to comment
Share on other sites


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...