Glaydur

Trigger once on overlap

7 posts in this topic

So I was following the flappy box tutorial over at lessmilk and I wanted to make a detector box for incrementing score instead of looking at how many pipes have spawned. So I put the detectors where the holes are suppose to be and I check for an overlap between the bird and also the detector box so if it touches, +1 to the score. The problem is, since game.physics.overlap(...) triggers every frame, the score increments more than once. So is there any way for me to just trigger the overlap once and therefore increment the score only once?

 

relevant code in my game.js:

update: function() {    // Executed 60 times per second || Contains game logic    // Call restartGame if bird is out of screen(too high or too low)    if(this.bird.inWorld === false) {        this.restartGame();    }    game.physics.arcade.overlap(this.bird, this.pipes, this.restartGame, null, this);    game.physics.arcade.overlap(this.bird, this.detectors, this.updateScore, null, this);},
updateScore: function() {    this.score += 1;     this.labelScore.text = this.score;}

Or maybe there's a function that sets an it to a trigger similar to that in Unity3D?  :D

Share this post


Link to post
Share on other sites

I'm having a similar problem as Glaydur, but I also have an animation that plays after the first overlap, and before the sprite gets killed. The overlap gets triggered while the animation is playing, which is affecting the scoring of my game. 

 

Is there a way to keep the animation, and turn off physics or something after the first overlap?

 

This is the code I'm using to play the animation and kill the sprite:

goodItem.animations.play('explode', 10, false, true);goodItem.animations.killOnComplete = true;

Share this post


Link to post
Share on other sites

The most simple way to do this is to just to call .kill() on the score triggering object in the callback. In a Flappy Bird scenario, the object scrolls past and is usually killed on the left edge of the screen anyway, then typically an object pool is in place to revive it and put it back on the right, where it can live again to score another day :)

updateScore: function(bird, detector) { 
  // First ensure the detector we're hitting isn't dead already
  if (!detector.alive) {
    return;
  }
  // Okay, it is alive, so kill it and increment the score!
  detector.kill();
  this.score += 1;
  this.labelScore.text = this.score;
}

 

Share this post


Link to post
Share on other sites

Checking for and setting an arbitrary property on one or both of the colliding sprites ought to do it. Something like this: https://phaser.io/sandbox/edit/CyBbKymD (check the update tab for the overlapHandler method which does the magic). Note that hasOverlapped isn't a Phaser property, just a flag I'm manually creating on the overlapping sprites.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.