AramCP

Bullets not shooting properly

Recommended Posts

Hi, im really new to phaser3, im trying to do a space invaders game, now I was creating the bullets group, and what I wanted to do is that when you press Space, it shoots one bullet, but instead of that, when I press space it keeps creating bullets till I lift the key. This is how my code looks like:

 

function create (){
      bullets = this.physics.add.group();
      this.spaceKey = this.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.SPACE);
}

function update (){
if (this.spaceKey.isDown){
    firebullet();
}

function firebullet(){
        bullets.create(player.x, 290, 'bullet');
	bullets.setVelocityY(-300);
}

 

Share this post


Link to post
Share on other sites

When you fire a bullet you need to record the time. You can get this as the first argument to the update function (i.e. make it function update (time))

You then don't allow them to fire again until X ms have passed. What X is, is up to you. Try around 200 for a slow-fire, 50 for a faster one, etc.

Share this post


Link to post
Share on other sites
10 minutes ago, rich said:

When you fire a bullet you need to record the time. You can get this as the first argument to the update function (i.e. make it function update (time))

You then don't allow them to fire again until X ms have passed. What X is, is up to you. Try around 200 for a slow-fire, 50 for a faster one, etc.

In my update function I also have the player movement so putting a timer there would make my player move weird (I think). So maybe I can put that timer in the firebullet function? But i dont really know how to do that. I've searched for the Phaser 3 timer and I put this in my create function, but dont know what to do now...

timedEvent = this.time.addEvent({ delay: 300, callback: onEvent, callbackScope: this});

 

Share this post


Link to post
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...

  • Recently Browsing   0 members

    No registered users viewing this page.