collectStar
has not been passed any arguments there:
this.physics.add.overlap(player, stars, collectStar, null, this);
because it is not called there, collectStar
has just been passed as an argument to the this.physics.add.overlap
function. Somewhere inside the definition of this this.physics.add.overlap
function it will be called with arguments as the two colliding objects or groups. Same for the hitBomb
function case you have asked. It is the way the this.physics.add.overlap
is defined.
-
It is up to you to use the colliding two entities, two objects or two groups of objects, passed as arguments to your collision detection callback, here
collectStar
, by the framework or not. If you want to use them define your callback with two arguments. I guess if you define with one then you will only have access to the one of them inside the callback function, and this is JavaScript related. -
The objects that is created in the
create()
may not be the colliding or overlapping objects. Yes here you have one player. It is the player but there are many stars. You will need to know which one you interact at a moment. So the framework does this for you by passing these two entities as arguments to your callback. They are,player
andstar
, the very objects that has just collided and this collider callback,collectStart
, has been called. In case you need the start in the callback then you might avoid passing the player since it is the player but not same for the colliding star only if you have known the order framework will pass the colliding objects to the callback and it is the one you need, herethis.player
. But I advice you to never do that, it is convoluted and really unnecessary. If you really need to do that you really don’t. I would say you are really in an anti-pattern.
Collision callback functions, here collectStar
and hitBomb
, are defined to be passed the colliding two game objects, or groups. It is the way they have been defined in the Phaser and this is a very useful and logical thing to do when defining a collision callback function. One reason is you will probably need access to the objects that have actually collided, here the star among all the starts and the player. Same for overlaps.