Phaser 3: Collider Issue - error after scene reloads

Hello everyone, I’m having a bug I can’t seem to squash. Short and simple, I have spikes in my game, when the player collides with the spikes, after 2 seconds, the scene will reload. That is working. After the scene reloads, when I collide with the spikes again, I get an error. It shows to be an error in the phaser.min.js file. Below is a GIF I created to show what happens:

I have a Fail Class that specifies what happens when a player fails on a hazard/obstacle. I created a class so that I can have one chunk of code to re-use regardless of what kind of death the player experiences.

Here is the code for the Fail constructor:

export default class Fail {

    constructor(scene, level) {
        this.scene = scene;

        this.promptFail = () => {
            this.failText = scene.add.text(this.scene.width / 2, this.scene.height / 2, level, {fill: "white", fontSize: "75px", fontFamily: "pixel1"});
            this.scene.scene.start(this.scene);
            console.log("Restart level");
        }
        scene.time.delayedCall(2000, this.promptFail, null, scene); // timer that removes the notify text after 4 seconds
    }
}

Here is the code in my level that implements the Fail constructor:

this.physics.add.collider(this.player.player, this.spikes, this.fail, null, this);

fail() {
        this.fail = new Fail(this, "level1");
    }

I’m not sure but you may need to change that to

this.fail = new Fail(this, "level1");

this.physics.add.collider(this.player.player, this.spikes, this.fail, null, this);

Thanks for the quick response. So I attempted that and got rid of the fail() method entirely.

Now I get the error immediately on the first collision with the spikes. Wonder if it has something to do with having a separate class/constructor?

phaser.min.js:1 Uncaught TypeError: i.call is not a function
    at initialize.collideSpriteVsGroup (phaser.min.js:1)
    at initialize.collideHandler (phaser.min.js:1)
    at initialize.collideObjects (phaser.min.js:1)
    at initialize.update (phaser.min.js:1)
    at initialize.step (phaser.min.js:1)
    at initialize.update (phaser.min.js:1)
    at initialize.h.emit (phaser.min.js:1)
    at initialize.step (phaser.min.js:1)
    at initialize.update (phaser.min.js:1)
    at initialize.step (phaser.min.js:1)

I take that back, do this instead:

this.physics.add.collider(
  this.player.player,
  this.spikes,
  function () {
    this.fail = new Fail(this, "level1");
  },
  null,
  this
);

That did it! Thanks @samme

Do you happen to know why the way I had it was causing issues? I’m not sure I understand that error stack.

Somehow this.fail was defined during the first scene run but undefined after restart. But regardless, you wouldn’t want to use this.fail (a Fail instance) as a collision callback; instead you would instantiate a new Fail in the callback.

If you switch to phaser.js then the traces will be a little easier to read.

1 Like

I need to do that. I’m afraid if I change to phaser.js everything will break. Have you had any experience switching from one to the other?

It shouldn’t break if both are the same Phaser version. Here is v3.17.0:

https://www.jsdelivr.com/package/npm/phaser?version=3.17.0&path=dist

1 Like

Thanks I’ll try it out.