Argument order of this.physics.add.overlap() in Arcade Physics

Hi all,

Pulling my hair out on this one. In the code below, everything works (renders as expected, collision works) fine.

Except for one thing: the two lines (one commented) that create the overlap object seem interchangeable. Swapping the first two arguments does not appear to change the order in which they are received by the process function. Either way, the player (that phaser dude in a sprite) comes in first (observed by the type being sprite and the key of the texture).

I must be doing something elementary wrong, but it escapes me… One could say “use it the way it works” but that feels downright wrong. Anyone any idea?

Thanks in advance,
Manno

    dude = this.physics.add.sprite(100, 100, "dude");

    var map = this.make.tilemap({
        key: "map",              
        tileWidth: 64,            
        tileHeight: 64,           
        width: 16,                 
        height: 8,                  
    });

    var tileset = map.addTilesetImage("tileset");

    var platforms = map.createStaticLayer("baselayer", tileset, 0, 0);
    var trapLayer = map.createStaticLayer("death", tileset, 0, 0);

    platforms.setCollisionByProperty({collidable: true});
    trapLayer.setCollisionByProperty({damage: true});

    this.physics.add.collider(dude, platforms);
    
    // this.physics.add.overlap(dude, trapLayer, doDamage, process, this);
    this.physics.add.overlap(trapLayer, dude, doDamage, process, this);
}

function process(player, tile){
    return tile.properties.damage;
}

function doDamage(player, tile){
    console.log(player);
}

You’re not wrong, this is true of all sprite vs. group or sprite vs. layer collisions.

I always pass the sprite first just to make it consistent.

4 Likes

Ah, great. I was starting to doubt my sanity. Thanks for the reassurance :grin: (still feels odd though)

1 Like