Pausing and delaying a resumed scene

Trying to work out how to set a timer off when the ball goes out of bounds in a pong game, at the moment its automatically relaunching instantly. Been going with a scene pause and resume with set interval a few different ways.
Always starting with…
if (this.ball.body.x < this.playerPaddle.body.x - 25) {
enemyCurrentScore++;
this.scene.restart();
this.scene.pause();

        setInterval(function () {
          this.scene.resume;
        }, 3000);

And

        this.time.delayedCall(
          3000,
          function () {
            return this.scene.resume;
          },
          [],
          this
        )

also I have

    this.resumeGame = function () {
      this.pauseText = this.add.text(250, 200, "NEW GAME", {
        fontSize: "64px",
        fill: "#fff",
      });

Which is the function inside the other.
Would appreciate any help, been on this for hours :smiley:

Try using setTimeOut instead:

setTimeOut(()=>{ //(function, time)
           this.scene.resume;
},3000);
1 Like

:wave:

You must write callbacks as

setTimeout(() => {
  this.scene.restart();
}, 3000);

Likewise

this.time.delayedCall(3000, this.scene.restart, [], this.scene);

or

this.time.delayedCall(
  3000,
  function () {
    this.scene.restart();
  },
  [],
  this
);

It looks like you want to do pause then restart after delay. This is a little tricky because a paused scene doesn’t advance its clock so timer events (as in delayedCall()) will not be called.

You can do a fake pause:

gameOver = true;
this.scene.physics.world.pause();
this.time.delayedCall(3000, this.scene.restart, [], this.scene);

Or you can use setTimeout() (not setInterval(), which is a repeating interval):

setTimeout(() => {
  this.scene.restart();
}, 3000);
1 Like

Unfortunately I couldn’t get this to work. After the setTimeout(this.scene.restart, 3000) I get an error after the 3seconds saying:

Uncaught TypeError: Cannot read property ‘queueOp’ of undefined
at restart (phaser.js?d4ef:198993)

I wasn’t passing context correctly. :frowning: I corrected the post above.

1 Like

Thank you so much, headache over :smiley: