AramCP Posted September 24, 2018 Share Posted September 24, 2018 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); } Link to comment Share on other sites More sharing options...
rich Posted September 24, 2018 Share Posted September 24, 2018 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. blackhawx 1 Link to comment Share on other sites More sharing options...
AramCP Posted September 24, 2018 Author Share Posted September 24, 2018 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}); Link to comment Share on other sites More sharing options...
rich Posted September 24, 2018 Share Posted September 24, 2018 Like this (click Edit to see the code) http://labs.phaser.io/view.html?src=src/pools/bullets.js&v=dev AramCP and durandaimen1997 2 Link to comment Share on other sites More sharing options...
Recommended Posts