Jump to content

How to "kill" one?


Ivasik
 Share

Recommended Posts

Hi guys!  I have:

    pass_round: function() {
        live = lives.getFirstAlive();
        
        if (live) {
            live.kill();
        }
        
        if (lives.countLiving() < 1) {
            this.game.state.start('mainmenu');
        }
    },

//and

    update: function() {      
        if (this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR).duration > 1000) {
            this.pass_round();
        }
    }

and after holding spacebar lost all lives, instead of one.  How to solve this problem?

Link to comment
Share on other sites

If you continue to just hold the spacebar, the conditional in your if statement will always evaluate to true, calling pass_round every single time the update loop is called.

You can solve this in many ways. Here are a few ideas:

  • You could add a flag that needs to be checked before pass_round is called that is reset when a spacebar onUp event is fired.
  • You could set the onDownCallback property on Phaser.Keyboard and check for the spacebar being pressed. This would allow you to quickly tap the spacebar to trigger pass_round, but if you want to limit calls to pass_round to once every 1000ms, you'll have to set a simple timer up.
  • You could set a timer in pass_round to ensure it only performs its action once every given period of time.

You can search through the Phaser examples for games that you think might have similar mechanics to yours to see how they solve this issue. Here's a snippet from one such example showing how to limit a bullet to fire once every 150ms:

function fireBullet () {

    if (game.time.now > bulletTime)
    {
        bullet = bullets.getFirstExists(false);

        if (bullet)
        {
            bullet.reset(sprite.x + 6, sprite.y - 8);
            bullet.body.velocity.y = -300;
            bulletTime = game.time.now + 150;
        }
    }

}
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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