Jump to content

Bullet spawn speed or generation speed


Rafaelx
 Share

Recommended Posts

Hi all I have a function that generates bullets every time the player touch the screen, is there a way that I can limit the amount of bullets generated, basically if I press the screen very quickly lots of bullets get generated but I would like to limit it to at least i per sec instead of 2 or 3 per sec something like that, bellow you can find my firing function and my bullet creation function, any help I would really appreciate:


    createBullets: function(){
        
                //Bullets
        this.bullets = this.add.group();
        this.bullets.enableBody = true;
        this.bullets.physicsBodyType = Phaser.Physics.P2JS;
        this.bullets.createMultiple(500, 'bullet', 0, false);
        this.bullets.setAll('anchor.x', 0.5);
        this.bullets.setAll('anchor.y', 0.5);
        this.bullets.setAll('outOfBoundsKill', true);
        this.bullets.setAll('checkWorldBounds', true);    

      
        
    },



    fireBullet: function(){
        
        this.bullet = this.bullets.getFirstExists(false);
        if (this.bullet) {
            this.bullet.reset(this.tanque.x, this.tanque.y - 20);
            this.bullet.body.velocity.y = -500;
        }
        
    },

and my update function:

    
        
        if(this.input.activePointer.isDown){
         if (!this.mouseTouchDown) {   
            this.touchDown();
        }
        }else {
            if (this.mouseTouchDown) {
                this.touchUp();
            }
        }

 

Link to comment
Share on other sites

You'll want to set up a timer to see if the game is ready to fire a bullet. Here's how I'd try to do it at first:

Set a timer variable, something like:  this.bulletTimer = 0;

Surround the code in your fireBullet function with a check to see if enough time has passed:

fireBullet: function(){

    // Check to see if a second or more has passed
    if (this.bulletTimer < this.game.time.time) {
        // Set the timer to now + 1000ms (1 second)
        this.bulletTimer = this.game.time.time + 1000;
        // Fire the bullet
        this.bullet = this.bullets.getFirstExists(false);
        if (this.bullet) {
            this.bullet.reset(this.tanque.x, this.tanque.y - 20);
            this.bullet.body.velocity.y = -500;
        }
    }

},

This will limit the bullet to firing only once per second. You can adjust how long the timer lasts by adjusting this.game.time.time + 1000 to whatever you'd like. Just express your time in milliseconds.

Let me know if this doesn't work for you.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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