How to create/spawn multiple bullets at same time

rgsgdf

I have a ship with 2 weapons on their body.

So, i decide to duplicate these function with custom parameter for weapon position.

bullet.fire(ship.x, ship.y, 1);
bullet.fire(ship.x, ship.y, 2);

But the result is not as i expected, Only one bullet are created (last function / 2), i think bullet 1 position are merge with bullet 2 position. since this function are called 2 times.

How to do this? i want to spawn bullet at multiple image points at same time.

Thanks

UPDATE:

bullet.fire(ship.x, ship.y);
setTimeout(function(){
bullet.fire(ship.x, ship.y);
},500)

So, first bullet will be destroyed if second bullet are created, i don’t know why this happen.

bullet.fire(ship.x, ship.y, 1);
bullet.fire(ship.x, ship.y, 2);

I am not surprised but according to the tiny code you are sharing, u create two bullets at the same coordinates… only difference which u, not even explaining is the third argument 1 and 2?

You need to create two bullets, separately.

Hi @donjon1234 ,
I don´t know if bullet represents a bullet object or a group of bullets…
If you haven’t created a group of bullets (pool) then you need to create it before using the bullets.
Here an example: http://labs.phaser.io/view.html?src=src\pools\bullets.js

In your case simply take two bullets from the group in each shoot.

Regards.

It’s a parameter to identity it’s position. ex: 1 == left weapon, 2 == right weapon.

Can you give me an example code for 2 bullets ? or how to call it properly

var bullet = bullets.get().setActive(true).setVisible(true);
var bullet2 = bullets.get().setActive(true).setVisible(true);
if (bullet) {
bullet.fire(player, data, 1);
bullet2.fire(player, data, 2);
}

Like this ?
It’s working on first shot only, next shot only spawn 1 bullet

Hi @William_Soeparjo,
I had the same problem editing the phaser example http://labs.phaser.io/edit.html?src=src\pools\bullets.js .
One direct solution is make two groups of bullets, one for each weapon.

Applied to the example:

   // In create function
   ///////////////////////////////
    bullets = this.add.group({
        classType: Bullet,
        maxSize: 10,
        runChildUpdate: true
    });
    bullets2 = this.add.group({
        classType: Bullet,
        maxSize: 10,
        runChildUpdate: true
    });

    // In update function
    /////////////////////////////////
    if (cursors.up.isDown && time > lastFired)
    {
        var bullet = bullets.get();
        var bullet2 = bullets2.get();

        if (bullet)
        {
            bullet.fire(ship.x, ship.y);
            bullet2.fire(ship.x + 10, ship.y);
            lastFired = time + 50;
        }
    }

Regards.

Wow, thank you! it’s working.