Passing argments into functions

Refering to the First Phser3 game …

I notice in the overlap we just call collectStar without passing any parameters to the function and in the collectStar function, sudenly there is player , star ( function collectStar (player, star) )

A few basic questions …

  1. Why is player passed into the function and not used in the function ?

  2. If I use this.player and this.star in create(), do I still need to passed in this.star into the function ??

this.physics.add.overlap(player, stars, collectStar, null, this);

function collectStar (player, star)
{
    star.disableBody(true, true);
}

Again for part 10 http://phaser.io/tutorials/making-your-first-phaser-3-game/part10
bomb is passed into hitBomb but never used in the function… Why is this so ??

this.physics.add.collider(player, bombs, hitBomb, null, this);

function hitBomb (player, bomb)
{
    this.physics.pause();
    player.setTint(0xff0000);
    player.anims.play('turn');
    gameOver = true;
}
1 Like

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.

  1. 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.

  2. 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 and star, 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, here this.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 :slight_smile:. 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.

1 Like

You don’t actually call collectStar in the overlap, instead you pass collectStar itself. The physics engine calls collectStar later if it detects a collision and it always passes the two colliding sprites.