Phaser3: How to react with a single member of a group
I have a bunch of snowmen. They are all tween. When a player is captured by a specific snowman it is carried by it.
It is messy, inconvenient and time wasting to create every single sprite and set it to tween. I have learnt to use
const snowman = this.physics.add.group(); snowman.create(x, y, 'key');
But this does not give each snowman a name. When I set the player's X and Y when it is being carried, I set it to the x and y of the snowman that hit him. If I use the code above, it will obviously no longer work. What can do instead?
Well without knowing your code. I would:
- Create a variable to mark the snowman, that captured the player
- and when the player collides with one snowman, set variable. Like this you know which snowman.
in the create
function:
function create(){
...
this.mainSnowMan = undefined; // at the begining no snowman is selected
this.physics.add.collider(player, snowman, (player, currentsnowman) => {
...
this.mainSnowMan = currentsnowman; // set the snowman that collided with the player
});
...
}
documenation to the collider
function https://photonstorm.github.io/phaser3-docs/Phaser.Physics.Arcade.Collider.html
and here is a link so man phaser physics examples: https://phaser.io/examples/v3/category/physics/arcade