Collider accepts StaticPhysicsGroup but passes ArcadeSprite to callback

I’m new to Phaser and was looking at the breakout example and I came across a curiosity.

When a Collider is added we are passing in the bricks StaticPhysicsGroup, and the callback seems to use an ArcadeSprite object reference from within the StaticPhysicsGroup, children entries array . The callback then uses the StaticPhysicsGroup referenced from the Scene later in a conditional within that callback.

// add the bricks static physics group -  line 27
this.bricks = this.physics.add.staticGroup({
        key: 'assets', frame: [ 'blue1', 'red1', 'green1', 'yellow1', 'silver1', 'purple1' ],
        frameQuantity: 10,
        gridAlign: { width: 10, height: 6, cellWidth: 64, cellHeight: 32, x: 112, y: 100 }
    });

// add the collider and pass in the StaticPhysicsGroup passing in the hitBrick callback- line 39
this.physics.add.collider(this.ball, this.bricks, this.hitBrick, null, this);

// hit brick call back on line 66 - see the brick reference (this should be the StaticPhysicsGroup
// but is an ArcadeSprite from the StaticPhysicsGroup children entries array - why?
hitBrick: function (ball, brick)
{
    // Using the ArcadeSprite reference
    brick.disableBody(true, true);

    // using the StaticPhysicsGroup from the Scene reference (why does this not come through as a param in the callback?
    if (this.bricks.countActive() === 0)
    {
        this.resetLevel();
    }
},

This seem Odd. Why is the StaticPhysicsGroup passed to the collider but yet the ArcadeSprite is the reference that is used in the param list? How do we pass in the StaticPhysicsGroup and use the StaticPhysicsGroup as a reference so we don’t have to get it from the scene?

can we get a reference to both in the param list. e.g. ?

callback(ArcadeSprite1, ArcadeSprite2, StaticPhysicsGroup){...}

Thanks in advance.

It’s a feature :smile: Few people are interested in the group (probably).

Not without changing Phaser.
You can add a property to each group member though:

for (i = 0; i < group.getChildren().length; i++) {
    group.getChildren()[i].group = group;
}

Now you can just check brick.group.

1 Like

collisionCallback represents the collision, so it’s passing the colliding objects.

You can wrap the callback and pass the group yourself:

this.physics.add.collider(
  this.ball,
  this.bricks,
  function (ball, brick) {
    this.hitBrick(ball, brick, this.bricks);
  },
  null,
  this
);
1 Like

Thanks for the info Milton,

I was just looking at it from the perspective of decoupling the callback from being a member of the scene, so for example I could create a separate file/module with the callback (does not have to live as a member of the actual scene class), then if I had multiple scenes that wanted to use this functionality I could just pass the callback to each instance of the this.physics.add.collider() method, then the call back not only has a reference to the objects that collide, but also has reference to the context, e.g. the scene and thus the group in that scene from within that decoupled callback. This is why I said it seems odd.