【问题标题】:Phaser 3 Trying to trace number of bullets activePhaser 3 试图追踪活动子弹的数量
【发布时间】:2022-10-23 13:01:43
【问题描述】:

我正在尝试将我的子弹变成group。我是 Phaser 3 的初学者。我的子弹从here 开始工作。我想让创建的子弹可追溯。

还有一个问题。经过一些简短的测试后,我发现创建了多个项目符号。测试1:我在场景外部添加了障碍物,并添加了与子弹和障碍物的碰撞器,触发时它会记录多次。测试2:在与敌人接触时,一些子弹被反射并飞向另一个方向。这与问题有关,因为如果有某种方法可以追踪它们,这可以通过限制子弹来完成。

点击here查看replit的完整代码。

this.input.on('pointerdown', pointer => {
    if (ammo > 0) {
        charge.setText('CHARGED!');
        let speed = 750;
        console.log(ammo);

        // create bullet
        bullet = this.add.image(playerArm.x, playerArm.y, 'bullet');
        bullet.setScale(0.5);
        bullet.rotation = playerArm.rotation;
        this.physics.add.existing(bullet);

        group = this.add.group({
            defaultKey: 'bullet',
            maxSize: 100,
        })

        // get Vector where to shoot bullet
        let vector = new Phaser.Math.Vector2(pointer.x - playerArm.x, pointer.y - playerArm.y);

        // set Speed of bullet 
        vector.setLength(speed * timeSpeed);

        // QuickFix to destroy the bullet after 1 Second automatically
        setTimeout(() => bullet.destroy(), 1500);
        ammo -= 1;

        // to shoot in a straightline, just comment the following line in
        // bullet.body.setAllowGravity(false);
        function killBullet() {
            bullet.destroy();
        }


        bullet.body.setVelocity(vector.x, vector.y);
        bullet.body.setAllowGravity(false);
        bulletsAlive.setText('Bullets alive' + group.getLength())
        this.physics.add.collider(bullet, enemy, hitEnemy, null, this);
        this.physics.add.collider(bullet, platforms, killBullet, null, this);
    }
});

if (ammo <= 0) {
    function onEvent() {
        ammo = 15;
    }
    timedEvent = this.time.delayedCall(2000, onEvent, [], this);
    // setTimeout(() => ammo = 15, 1000);
    charge.setText('OUT OF CHARGE! CHARGING!');
    console.log('reloading');
}

【问题讨论】:

  • 我只是想问一下,我的回答有帮助吗,还是我错过了什么?
  • 对不起,伙计,忘了批准它;-;。你的回答肯定有帮助!

标签: javascript phaser-framework


【解决方案1】:

多个项目符号问题与pointerdown-event 侦听器代码在update 函数中的事实有关update 函数或多或少被调用每秒 60 次).
这意味着,每秒大约有 60 个事件侦听器被附加,因此对于每个click 事件,此事件侦听器将被多次调用。(随着时间的流逝,也可能导致性能问题).

只需将此代码从 update 函数中移出:

   this.input.on('pointerdown', pointer => {
    if (ammo > 0) {
        charge.setText('CHARGED!');
        let speed = 750;
        console.log(ammo);
        // create bullet
        let bullet = this.add.image(playerArm.x, playerArm.y, 'bullet');
        bullet.setScale(0.5);
        bullet.rotation = playerArm.rotation;
        this.physics.add.existing(bullet);
        // get Vector where to shoot bullet
        let vector = new Phaser.Math.Vector2(pointer.x - playerArm.x, pointer.y - playerArm.y);
        vector.setLength(speed * timeSpeed);
        setTimeout(() => bullet.destroy(), 1500);
        ammo -= 1;
        // to shoot in a straightline, just comment the following line in
        // bullet.body.setAllowGravity(false);
        function killBullet() {
            bullet.destroy();
        }
        bullet.body.setVelocity(vector.x, vector.y);
        bullet.body.setAllowGravity(false);
        this.physics.add.collider(bullet, enemy, hitEnemy, null, this);
        this.physics.add.collider(bullet, platforms, killBullet, null, this);
    }
});

进入create函数,只会创建一个子弹,现在ammo 变量应该有一个准确的子弹计数。

这似乎是您最初的计划,因为在 create 函数中,您已经有一个用于 pointerdown 事件的空事件监听器:
this.input.on('pointerdown', function (pointer) {
// shoot the bullet towards the cursor (player1Aim)
});

小费:我建议删除所有不需要、未使用或重复的代码,因为这将使您的开发和调试更容易。

【讨论】:

    猜你喜欢
    • 2022-10-05
    • 2015-08-31
    • 2021-06-20
    • 2020-11-09
    • 2018-02-24
    • 1970-01-01
    • 2011-02-26
    • 2021-03-13
    • 1970-01-01
    相关资源
    最近更新 更多