子弹生成速度或生成速度相位器
Bullet spawn speed or generation speed phaser
大家好我有一个功能,每次玩家触摸屏幕时都会生成子弹。
有什么方法可以限制生成的子弹数量吗?基本上,如果我非常快地按下屏幕,就会产生很多子弹,但我想将其限制为每秒至少 i
而不是每秒 2 或 3 发。
下面是我的开火函数和子弹创建函数:
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;
}
},
和我的更新功能:
if(this.input.activePointer.isDown){
if (!this.mouseTouchDown) {
this.touchDown();
}
}else {
if (this.mouseTouchDown) {
this.touchUp();
}
}
如有任何帮助,我将不胜感激。
一个选项是存储两个值:
- nextShotTime:下次可以开枪的时间
- shotDelay:拍摄之间的延迟(可以像
Phaser.Timer.SECOND * 2
一样设置)
我看不到你在上面的示例代码中调用 fireBullet()
的位置,但是在调用之前或在函数中,你可以检查 nextShotTime
是过去。如果是,发射另一颗子弹,然后用当前时间加上 shotDelay
.
更新 nextShotTime
例如:
if (this.nextShotTime < this.time.now) {
this.nextShotTime = this.time.now + this.shotDelay;
// add your code that will fire a bullet.
}
我以前玩游戏也遇到过类似的问题。我使用的解决方案与上面发布的解决方案相同。我在这个找到了
Phaser tutorial.
教程中使用的开火函数是:
fire: function() {
if (this.nextShotAt > this.time.now) {
return;
}
this.nextShotAt = this.time.now + this.shotDelay;
您可以根据自己的目的对其进行修改。
这是我在自己制作的游戏中使用的射击函数的一部分:
fire: function() {
//Make sure the player can't shoot when dead and that they are able to shoot another bullet
if(!this.player.alive || this.time.now < this.nextFireTime) {
return;
}
this.nextFireTime = this.time.now + this.fireRate;
var bullet;
//If weaponlvl = 0, shoot a normal bullet
if(this.weaponlvl === 0) {
if(this.bullet1.countDead() === 0) {
return;
}
//Properties for the basic weapon
this.fireRate = 500;
bullet = this.bullet1.getFirstExists(false);
bullet.reset(this.player.x, this.player.y-22);
bullet.body.velocity.y = -300;
}
希望这对您有所帮助。
大家好我有一个功能,每次玩家触摸屏幕时都会生成子弹。
有什么方法可以限制生成的子弹数量吗?基本上,如果我非常快地按下屏幕,就会产生很多子弹,但我想将其限制为每秒至少 i
而不是每秒 2 或 3 发。
下面是我的开火函数和子弹创建函数:
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;
}
},
和我的更新功能:
if(this.input.activePointer.isDown){
if (!this.mouseTouchDown) {
this.touchDown();
}
}else {
if (this.mouseTouchDown) {
this.touchUp();
}
}
如有任何帮助,我将不胜感激。
一个选项是存储两个值:
- nextShotTime:下次可以开枪的时间
- shotDelay:拍摄之间的延迟(可以像
Phaser.Timer.SECOND * 2
一样设置)
我看不到你在上面的示例代码中调用 fireBullet()
的位置,但是在调用之前或在函数中,你可以检查 nextShotTime
是过去。如果是,发射另一颗子弹,然后用当前时间加上 shotDelay
.
nextShotTime
例如:
if (this.nextShotTime < this.time.now) {
this.nextShotTime = this.time.now + this.shotDelay;
// add your code that will fire a bullet.
}
我以前玩游戏也遇到过类似的问题。我使用的解决方案与上面发布的解决方案相同。我在这个找到了 Phaser tutorial.
教程中使用的开火函数是:
fire: function() {
if (this.nextShotAt > this.time.now) {
return;
}
this.nextShotAt = this.time.now + this.shotDelay;
您可以根据自己的目的对其进行修改。
这是我在自己制作的游戏中使用的射击函数的一部分:
fire: function() {
//Make sure the player can't shoot when dead and that they are able to shoot another bullet
if(!this.player.alive || this.time.now < this.nextFireTime) {
return;
}
this.nextFireTime = this.time.now + this.fireRate;
var bullet;
//If weaponlvl = 0, shoot a normal bullet
if(this.weaponlvl === 0) {
if(this.bullet1.countDead() === 0) {
return;
}
//Properties for the basic weapon
this.fireRate = 500;
bullet = this.bullet1.getFirstExists(false);
bullet.reset(this.player.x, this.player.y-22);
bullet.body.velocity.y = -300;
}
希望这对您有所帮助。