Jump to content

Calling signals based on evaluation


MrOrphanage
 Share

Recommended Posts

Hi all,

I've just found out about signals but I'm having some difficulty implementing them. I came across them while searching for a solution to a problem I'm having: namely I'm trying to call a function when two variables decrement down to 0. So, when "pTurnsLeft == 0 && cTurnsLeft == 0" then the function should call once. I initially threw this check in to the update statement, and it worked for a bit. However, I'm now having the issue that the function is constantly called. This wasn't any issue at first for me, because after the function ran, I reset the entire play state to start the game over. However, I'm trying to change the handling of how to reset the game so that instead of re-calling the play state completely (and losing all the variable changes that were made in the round) I just manually reset the variables I want to so the game "starts over" manually. The issue at hand now though is since my update function is evaluating the above expression, it's calling my function 60 times/second and I'm not able to reset my game loop properly. This is where signals come in.

I've been led to believe that I can create a signal that evaluates the above expression and calls my function just once. This is obviously appealing as it would fix my issue, but I'm having trouble figuring out exactly where I need to tell the signal what expression to evaluate before it dispatches. Am I misunderstanding how signals work here or can I in fact use a signal in my play state to fire off a function when the expression I mentioned above is true?

Here's what I have so far:

var roundEnd = new Phaser.Signal();
roundEnd.add(this.callResults, this);

/* I'm not sure how but I need to do this:
roundEnd.dispatch() when "this.pTurnsLeft == 0 && this.cTurnsLeft == 0"*/

EDIT: For what it's worth, I've read through a couple of signals tutorials I found from googling but all seem to focus on dispatching things based on touch/mouse click events or key presses. I understand how to implement those, but I'm confused about how to go about implementing listeners for expression evaluation.

Link to comment
Share on other sites

The Signal itself doesn't do any evaluation, it just hooks up listeners and waits for you to trigger it.

You can use addOnce:

// @create:
var roundEnd = new Phaser.Signal();
roundEnd.addOnce(this.callResults, this);

// @update:
if (this.pTurnsLeft == 0 && this.cTurnsLeft == 0) {
  roundEnd.dispatch();
  return;
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

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