Scene postupdate event and the scene update() method problem

Hi all,

I’m recreating the classic game breakout and have stumbled across a problem.

When the ball falls out of the game (the y position is greater than the canvas height), code in my update method resets it and places it back on the paddle.

However, the ball is supposed to be centered and placed 10px above the paddle.

The ball is being centered just fine but the gap is only 5 or 6px.

When I put the same update() method code into a ‘postupdate’ event listener (and comment out the scene update method), it works fine. The gap is exactly 10px as I want.

Any ideas?

this.events.on('postupdate', function() {
  if (this.ball.y > (this.configData.world.height + this.ball.height)) {
    this.resetBall(this.ball);
    this.addBallToPaddle(this.ball, this.paddle);
  }
}, this);
resetBall(ball) {
  ball.setVelocity(0);
  ball.x = ball.initialX;
  ball.y = ball.initialY;
}
addBallToPaddle(ball, paddle) {
  ball.x = paddle.x;
  ball.y = paddle.y - (paddle.height + 10);
  
  ball.onPaddle = true;
}

Arcade Physics?

Yes, sorry I should have mentioned that.

I think you can do it in update() but use

function resetBall(ball) {
  ball.body.reset(ball.initialX, ball.initialY);
}
2 Likes

That fixed it. Thanks again samme, you’re my hero.

1 Like