How to update properties of Arcade.Group

Hi guys, I’m newbie, sorry for stupid questions, but…
I have a BombItem class and a BombsList class which is a collection of bombs.

class BombItem extends Phaser.Physics.Arcade.Image {
    constructor(config) {
        super(config.scene, config.x, config.y, config.key);

        this.scene.physics.world.enable(this);
        this.scene.add.existing(this);

        // !! -> Not working (overwritten) if added to the group:
        // this.setBounce(1);
        // this.setCollideWorldBounds(true);
        // this.setVelocity(Phaser.Math.Between(-200, 200), 20);
    }
}

class BombsList extends Phaser.Physics.Arcade.Group {
    constructor(config) {
        super(config.scene.physics.world, config.scene);

        // !! -> Overwrite defaults. Is that a correct way?
        this.defaults.setBounce = 1;
        this.defaults.setCollideWorldBounds = true;
        // this.defaults.setVelocityX = Phaser.Math.Between(-200, 200);
        this.defaults.setVelocityY = 20;
    }

    addBomb(x, y) {
        const newBomb = new Bomb({scene: this.scene, x, y, key: 'bomb'});
        // newBomb.setVelocityX = Phaser.Math.Between(-200, 200); // !! -> not working
        this.add(newBomb);
    }
}

I’m trying to update the properties like (setBounce and setCollideWorldBounds) of the Group by using the this.defaults access. So basically I just overwrite defaults. It works, but I’m not sure that this is the correct way to do it. Maybe there are some methods I can call to update the required properties? (There are no info about this in the documentation)

And the second question, how to set the velocity (or other property) to a newly created Bomb class inside my addBomb method?

Thank you!

Welcome Svyatik,
Changing the defaults of an Arcade Group is definitely a feasible way to do it. With that said, you can also add a third parameter to the super call in your Arcade.Group class.

So instead of:
super(config.scene.physics.world, config.scene);

it becomes:
super(config.scene.physics.world, config.scene, { bounceX: 1, velocityY: 20 });

As far as changing the physics properties of a child, you need to call the associated method. It seems as though you are treating the method like a property, which doesn’t work.

So instead of:
newBomb.setVelocityX = Phaser.Math.Between(-200, 200);

you should implement it like:
newBomb.setVelocityX(Phaser.Math.Between(-200, 200));

3 Likes

!! → Not working (overwritten) if added to the group: …

This happens because Arcade.Group applies its defaults after the new item is constructed.

1 Like

Thank you for your explanation, Jake.
It works now!

Just a note to those who will read my code, you should call such methods to update properties only after adding an object to a group otherwise it will not work (which is not obvious sometimes, I think)

addBomb(x, y) {
    const newBomb = new Bomb({scene: this.scene, x, y, key: 'bomb'});

    // first, add an object to a group
    this.add(newBomb);

    // and then update its properties
    newBomb.setVelocityX(Phaser.Math.Between(-200, 200));
}